python - 在Windows操作系统上使用Go和Python实现客户端-服务器模型

标签 python go client-server named-pipes

我正在开发一个GUI应用程序,UI是使用python(+kivy)开发的,核心是使用GoLang实现的。

我的应用程序涉及将数据从 UI 传递到 Core,为此我使用管道。以下是客户端和服务器代码片段。

客户端.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")) 

服务器.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)
}

使用上面的代码,我无法在它们之间建立连接。我的应用程序涉及客户端和服务器之间的双工通信

感谢任何形式的帮助!!

最佳答案

我无法找到使用管道的解决方案,因此我转移到套接字,并且我能够通过套接字进行通信。以下是代码片段

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))
    } 
}

上面的代码片段非常适合我的应用程序。

关于python - 在Windows操作系统上使用Go和Python实现客户端-服务器模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37442476/

相关文章:

python - 抽认卡抛硬币词典

go - 根据值匹配数组

go - JWT 认证策略

Android客户端与C服务器文件传输

c# - 如何使用 .NET 为 Windows 应用程序实现通知

python - 无法easy_install ssl模块

python - pandas 中的 tz_convert 不适用于印度的任何城市

python - scrapyd 中的类型错误

go - 如何读取3个json文件

JAVA使用类来同步线程之间的操作