How to send strings between python and go

huangapple go评论88阅读模式
英文:

How to send strings between python and go

问题

我正在尝试理解如何与用于 Python Socket 客户端的 Python Socket 服务器进行交互,但是使用 Go 语言。请使用相同的功能将 Python 客户端重写为 Go 语言,而不更改服务器代码,这样就足够让我理解如何实现了。

服务器:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = '127.0.0.1'
port = 5556

server.bind((host,port))
server.listen()
client, adress = server.accept()
#1
variable1 = client.recv(4096).decode('utf-8')
print(variable1)
#2
client.send("send2".encode('utf-8'))
#3
variable2 = client.recv(4096).decode('utf-8')
print(variable2)
#4
client.send("send4".encode('utf-8'))

客户端:

package main

import (
	"fmt"
	"net"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:5556")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.Close()

	//1
	conn.Write([]byte("send1"))
	//2
	buffer := make([]byte, 4096)
	n, err := conn.Read(buffer)
	if err != nil {
		fmt.Println(err)
		return
	}
	variable1 := string(buffer[:n])
	fmt.Println(variable1)
	//3
	conn.Write([]byte("send3"))
	//4
	n, err = conn.Read(buffer)
	if err != nil {
		fmt.Println(err)
		return
	}
	variable2 := string(buffer[:n])
	fmt.Println(variable2)
}

这是将 Python 客户端代码重写为 Go 语言的示例,具有相同的功能。希望这对你理解如何实现有所帮助。

英文:

I am trying to understand how to interact with python socket server meant for python socket client but in go

please rewrite python client in go language with same functionality without changing server code, that should be enough for me to understand how to do it

server:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = '127.0.0.1'
port = 5556

server.bind((host,port))
server.listen()
client, adress = server.accept()
#1
variable1 = client.recv(4096).decode('utf-8')
print(variable1)
#2
client.send("send2".encode('utf-8'))
#3
variable2 = client.recv(4096).decode('utf-8')
print(variable2)
#4
client.send("send4".encode('utf-8'))

client:

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = '127.0.0.1'
port = 5556

client.connect((host, port))


#1
client.send("send1".encode('utf-8'))
#2
variable1 = client.recv(4096).decode('utf-8')
print(variable1)
#3
client.send("send3".encode('utf-8'))
#4
variable2 = client.recv(4096).decode('utf-8')
print(variable2)

答案1

得分: 0

关闭连接以终止从服务器到客户端的数据流:

⋮
client.send("text1".encode('utf-8'))
client.close()

在客户端程序中读取到文件末尾(EOF):

⋮
message, err := io.ReadAll(conn)                
checkError(err)
fmt.Println(string(message))
英文:

Close the connection to terminate the stream of data from the server to the client:

⋮
client.send("text1".encode('utf-8'))
client.close()

Read to EOF in the client program:

⋮
message, err := io.ReadAll(conn)                
checkError(err)
fmt.Println(string(message))

huangapple
  • 本文由 发表于 2022年8月30日 21:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/73543386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定