无法转换' tuple'隐含地反对str

时间:2014-10-06 02:26:19

标签: python udpclient

我是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)

1 个答案:

答案 0 :(得分:0)

尝试以不受支持的方式合并strtuple会导致此错误

例如:

Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "foo" + ("bar",)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'tuple' object to str implicitly

我不认为您提供的代码示例正在执行此操作