Python与Go之间的字符串传输:将适配Python Socket服务器的Python客户端重写为Go客户端
Go Client Equivalent for Python Socket Server (No Server Modifications)
Hey there, I've converted your Python socket client to a fully functional Go client that works seamlessly with your existing Python server—no changes needed to the server code. Let's break down how string transmission between Go and Python works, since that's the core of what you're looking to understand.
Key Background for Cross-Language Socket Communication
- Both Python and Go default to UTF-8 for string-to-byte conversion, which is why we can communicate directly without encoding mismatches.
- Socket communication is byte-stream based, so the only work we need to do is convert strings to byte slices (and back) correctly on each end.
- Your Python server uses
recv(4096)to read chunks of data, so our Go client will mirror that buffer size for consistency.
Full Go Client Code
package main import ( "fmt" "net" ) func main() { // Establish TCP connection to the Python server conn, err := net.Dial("tcp", "127.0.0.1:5556") if err != nil { fmt.Printf("Failed to connect to server: %v\n", err) return } defer conn.Close() // Ensure connection is closed when we're done // Step 1: Send "send1" to the server (matches Python's client.send("send1".encode('utf-8'))) sendStr := "send1" _, err = conn.Write([]byte(sendStr)) if err != nil { fmt.Printf("Failed to send data: %v\n", err) return } // Step 2: Receive response from server (matches Python's variable1 = client.recv(4096).decode('utf-8')) buf := make([]byte, 4096) bytesRead, err := conn.Read(buf) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } variable1 := string(buf[:bytesRead]) fmt.Println(variable1) // Step 3: Send "send3" to the server sendStr = "send3" _, err = conn.Write([]byte(sendStr)) if err != nil { fmt.Printf("Failed to send data: %v\n", err) return } // Step 4: Receive final response from server bytesRead, err = conn.Read(buf) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } variable2 := string(buf[:bytesRead]) fmt.Println(variable2) }
Step-by-Step Mapping to Your Original Python Client
Let's align each part of the Go code with your Python client to make it crystal clear:
- Connecting:
net.Dial("tcp", "127.0.0.1:5556")does exactly what Python'sclient.connect((host, port))does—establishes a TCP connection to the server. - Sending Strings: In Go,
[]byte(sendStr)converts a string to a byte slice, which is identical to Python's.encode('utf-8')(since Go strings are UTF-8 by default).conn.Write()sends the byte slice, just like Python'sclient.send(). - Receiving Strings: We create a 4096-byte buffer (matching Python's
recv(4096)), read into it withconn.Read(), then convert the received bytes back to a string withstring(buf[:bytesRead]). ThebytesReadvariable ensures we only use the actual data received, not unused parts of the buffer—this mirrors Python's.decode('utf-8').
How to Use
- Start your Python server first.
- Save the Go code as
socket-client.go. - Run it with
go run socket-client.go.
You'll get the exact same behavior as the Python client: the Go client will print "send2" then "send4", and your Python server will print "send1" then "send3".
内容的提问来源于stack exchange,提问作者hecker
相关产品推荐
相关产品推荐

