-1
我是Python的新手,在使用Python创建客户端 - 服务器UDP ping服务器程序时遇到了这个严重错误。它说:无法将'元组'对象隐式转换为str
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)
哪一行给出错误? – 2014-10-06 02:28:00
当你分配'databin'时,你错过了't'吗? – squiguy 2014-10-06 02:28:41
请发布完整的追溯,也是你使用的是哪个python版本? – 2014-10-06 02:28:49