Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python套接字类型错误:应为str、bytes或bytearray,而不是int_Python_Python 3.x_Sockets - Fatal编程技术网

Python套接字类型错误:应为str、bytes或bytearray,而不是int

Python套接字类型错误:应为str、bytes或bytearray,而不是int,python,python-3.x,sockets,Python,Python 3.x,Sockets,根据我决定如何发送数据包,它会“改变”类型。例如,我将执行clientsocket.sendto(str.encode(packet),server_address),但它说它不需要int。。。 听起来很疯狂,但我会发布我的代码来显示发生了什么 print(type(packet)) print(type(encoded_packet)) print(type(packet.encode('utf-8'))) server_addr

根据我决定如何发送数据包,它会“改变”类型。例如,我将执行clientsocket.sendto(str.encode(packet),server_address),但它说它不需要int。。。 听起来很疯狂,但我会发布我的代码来显示发生了什么

        print(type(packet))
        print(type(encoded_packet))
        print(type(packet.encode('utf-8')))
        server_address = (host, port)
        print(type(server_address))
        clientsocket.sendto(packet.encode('utf-8'), server_address)
我收到一个错误,指出

TypeError: str, bytes or bytesarray expected, not int
这让我很困惑,因为我

print(type(packet.encode('utf-8'))) 
将其输出为

<class 'bytes'>
收到这个。。。(我知道上面的2行代码是错误的,我只想看看它输出了什么)

所以。。。我把它改成

encoded_packet = str.encode(packet)
clientsocket.sendto(str.encode(encoded_packet), server_address))
clientsocket.sendto(encoded_packet, server_address)
并收到此错误

TypeError: str, bytes, or bytearray expected, not int
这令人沮丧,任何帮助都将不胜感激。我尝试过许多不同的组合,但最后它“改变”了类型。 感谢您花时间阅读本文

编辑:第一个块的输出为

<class 'str'>
<class 'bytes'>
<class 'bytes'>
<class 'tuple'>
Traceback (most recent call last):
  File "./netster.py", line 118, in <module>
    main()
  File "./netster.py", line 100, in main
    run_client(args.host, int(args.port), udp=args.udp, rudp=args.rudp, filename=args.file) 
  File "./netster.py", line 46, in run_client
    rdt_send(port, port, kwargs['filename'])
  File "/Users/username/networks_repo/src/py/a3.py", line 234, in rdt_send
    clientsocket.sendto(packet.encode('utf-8'), server_address)
TypeError: str, bytes or bytearray expected, not int

回溯(最近一次呼叫最后一次):
文件“/netster.py”,第118行,在
main()
文件“/netster.py”,第100行,主
运行客户端(args.host,int(args.port),udp=args.udp,rudp=args.rudp,filename=args.file)
文件“/netster.py”,第46行,在run_client中
rdt_发送(端口,端口,kwargs['filename'])
文件“/Users/username/networks\u repo/src/py/a3.py”,第234行,在rdt\u send中
clientsocket.sendto(packet.encode('utf-8'),服务器地址)
TypeError:应为str、bytes或bytearray,而不是int

客户端套接字的类型是什么?你能输出第一个代码块的实时打印吗。我在本地试用了它,效果很好。
clientsocket
的类型是
。我编辑了原始帖子以显示第一块代码的输出。只是想检查一下,您是否尝试以UDP方式发送数据包?您能否检查主机对象的类型?您的主要问题似乎是
rdt\u send(port,port,kwargs['filename')
端口
被传递两次。也许你想写
rdt\u发送(主机、端口、kwargs['filename')
<class 'str'>
<class 'bytes'>
<class 'bytes'>
<class 'tuple'>
Traceback (most recent call last):
  File "./netster.py", line 118, in <module>
    main()
  File "./netster.py", line 100, in main
    run_client(args.host, int(args.port), udp=args.udp, rudp=args.rudp, filename=args.file) 
  File "./netster.py", line 46, in run_client
    rdt_send(port, port, kwargs['filename'])
  File "/Users/username/networks_repo/src/py/a3.py", line 234, in rdt_send
    clientsocket.sendto(packet.encode('utf-8'), server_address)
TypeError: str, bytes or bytearray expected, not int