【问题标题】:dataOutput.writeUTF Python EquivalentdataOutput.writeUTF Python 等效项
【发布时间】:2019-10-08 05:56:00
【问题描述】:

我正在尝试 Java 和 Python 之间的套接字连接。 Python 中的 dataOutput.writeUTF 是否等效于发送和接收数据?

这是我的代码示例。

received = client.recv(1024)
print(received)
toSend = input()
client.send(toSend)

【问题讨论】:

    标签: python dataoutputstream


    【解决方案1】:

    因为我有同样的问题,这里有一个解决它的代码示例:

    #!/usr/bin/env python3
    
    import socket
    import struct
    
    HOST = '127.0.0.1'  # The server's hostname or IP address
    PORT = 6666        # The port used by the server
    
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
    
        # Send message as UTF-8
        message = u"Hello from Python"
        data = bytearray(message, "utf8")
        size = len(data)
        s.sendall(struct.pack("!H", size))
        s.sendall(data)
    
        # Receive message
        data = s.recv(2)
        length = struct.unpack("!H", data)
        print("Length: {}".format(length))
        data = s.recv(1024)
        message = data.decode("utf-8")
        print("Received message: {}".format(message))
    
        s.close()
    

    我在这里找到了有用的信息:

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      相关资源
      最近更新 更多