2014-10-06 52 views
-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) 
+1

哪一行給出錯誤? – 2014-10-06 02:28:00

+0

當你分配'databin'時,你錯過了't'嗎? – squiguy 2014-10-06 02:28:41

+0

請發佈完整的追溯,也是你使用的是哪個python版本? – 2014-10-06 02:28:49

回答

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 

我不認爲你包括的代碼示例是這樣做的gh

+0

我該怎麼辦?導致uptil 4 pings它工作正常,但然後它顯示我的錯誤! – user2607744 2014-10-06 03:26:43

相關問題