【发布时间】:2023-03-18 09:50:02
【问题描述】:
我是 Python 新手,在 Python 中创建客户端-服务器 UDP ping 服务器程序时遇到了这个严重错误。它说:
TpyeError: Can't convert 'tuple' object to str implicitly
UDPClient.py文件中存在错误,即:
from socket import *
from datetime import *
from time import *
pings = 10
i =0
server = '127.0.0.1'
servPort = 5369
clientSock = socket(AF_INET, SOCK_DGRAM)
data = 'ping'
databin = bytes(data, 'UTF-8')
while i<pings:
print("Print number: ",i)
i += 1
before = datetime.now()
clientSock.sendto(databin, (server, servPort) )
clientSock.settimeout(1)
try:
clientMsgm server = cliendSock.recvfrom(1024)
after = datetime.now()
diff = before - after
if(i==10):
print("Ping", i)
except timeout:
print("Ping",i," Request timed out!!!")
Traceback (most recent call last):
File "D:\...\UDPClient.py", line 18, in <module>
clientSock.sendto(databin,server, servPort ) # Send what (i.e.'ping') and to whom and where
TypeError: an integer is required (got type str)
【问题讨论】:
-
哪一行给出了错误?
-
您在分配
databin时是否漏掉了一个“t”? -
请发布完整的回溯,以及您使用的是哪个python版本?
-
我猜错误一定在
byes之前 -
请始终复制和粘贴成绩单,而不是打字。
TpyeError中的错字让人很难知道byes中的错字是否有意义。