Python sendto告诉我它需要一个int

时间:2018-11-14 20:51:25

标签: python-3.x sockets

from socket import *
import packets
image = "testfile.bmp"

# open image
bufferSize = 2048
myfile = open(image, 'rb')
sequenceNumber = 0
totalBytes = 0

serverName = "127.0.0.1"
serverSendPort = 12000
serverSendPort = int(serverSendPort)
serverListenPort = 12001
clientSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket = socket(AF_INET, SOCK_DGRAM)

while (1):
    if (sequenceNumber == 0):
        data = packets.mkepckt(myfile.read(bufferSize), 0,         
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
        clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
        sequenceNumber = 1

        ACK,  = serverSocket.recvfrom(bufferSize)
        ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

        while (ACK[0] is not 0 or ACKchecksum is not ACK[2]):
            clientSocket.sendto(data, (serverName, serverSendPort))
            ACK,  = serverSocket.recvfrom(bufferSize)
            ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

    elif (sequenceNumber == 1):
        data = packets.mkepckt(myfile.read(bufferSize), 1,     
packets.calculateChecksum(myfile.read(bufferSize), bufferSize))
        clientSocket.sendto(bytearray(data),(serverName, serverSendPort))
        sequenceNumber = 0

        ACK,  = serverSocket.recvfrom(bufferSize)
        ACKchecksum = packets.checksum(bufferSize, ACK[1])

        while (ACK[0] is not 1 or ACKchecksum is not ACK[2]):
            clientSocket.sendto(data, (serverName, serverSendPort))
            ACK,  = serverSocket.recvfrom(bufferSize)
            ACKchecksum = packets.calculateChecksum(ACK[1], bufferSize)

myfile.close()

clientSocket.close()
serverSocket.close()

不确定我需要输入完整的代码,但比后悔更安全。 所以出于某种原因,尽管事实上我在sendto中有一个int。我还尝试了各种类型转换,从每个元素的类型转换到整个int的类型转换,我仍然会遇到此错误。

1 个答案:

答案 0 :(得分:0)

感谢@RemyLebeau的帮助,我能够通过导入pickle并使用它将我的元组转储到变量中,然后将该变量作为数据发送来解决此问题。不知道为什么字节数据会发出有关int的错误,但是您就可以了。