Rjain Rjain - 6 months ago 15
Python Question

Implementing Client- Sever model using Go and Python on Windows OS

I am working on an GUI application, the UI is developed using python(+kivy) and the core is implemented using GoLang.

My Application involves passing data from UI to Core, for which i am using pipes. Following is the snippet of Client and Server code.

Client.py:

p = win32pipe.CreateNamedPipe(r'\\.\pipe\test_pipe',
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE |win32pipe.PIPE_WAIT,
1, 65536, 65536,300,None)

win32pipe.ConnectNamedPipe(p, None)


data = "Hello Pipe"
win32file.WriteFile(p, bytes(data,"UTF-8"))


Server.go:

ln, err := npipe.Listen(`\\.\pipe\test_pipe`)
if err != nil {
// handle error
}


for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}

// handle connection like any other net.Conn
go func(conn net.Conn) {
r := bufio.NewReader(conn)
msg, err := r.ReadString('\n')
if err != nil {
// handle error
return
}
fmt.Println(msg)

}(conn)
}


With the above piece of code, i am not able to setup a connection between them. My Application involves duplex communication between client and server

Any sort of help is appreciated!!

Answer

i Could not find a solution using pipes, so i moved to sockets and i am able to communicate via sockets. Following is the snippets of the codes

Client.py

import socket
import struct

# create a socket object
requestCore = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM) 
responseCore = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM) 

# get local machine name
host = socket.gethostname()                           
port = 8100                                           

# bind to the port
requestCore.bind((host, port))                                  
# queue up to 5 requests
requestCore.listen(5)                                           
client,addr = requestCore.accept()

port = 9999
responseCore.connect((host, port))

while True:
    msg = input("User Input: ")
    client.send(bytes(msg.encode('UTF-8')))

    msgFrmServ = responseCore.recv(64)
    print("message from Server",msgFrmServ)

client.close()
responseCore.close()

server.go

package main 

import "net" 
import "fmt" 
import "bufio" 
import "os"
import "bytes"

func main() {   

    hostname,_ := os.Hostname()

    // connect to this socket   
    readconn, _ := net.Dial("tcp", hostname+":8100")   
    reader := bufio.NewReader(readconn)     

    ln, _ := net.Listen("tcp", hostname+":9999")   
    writeconn, _ := ln.Accept()    // accept connection on port   

    for { 
        text := make([]byte,64)
        reader.Read(text)
        text = bytes.Trim(text,"\x00")
        fmt.Println(string(text))

        Acktext := "Core Acknowledge: " + string(text)
        writeconn.Write([]byte(Acktext))
    } 
}

The above snippet works fine for my application.