【问题标题】:Can't convert 'tuple' object to str implicitly无法将“元组”对象隐式转换为 str
【发布时间】: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 中的错字是否有意义。

标签: python udpclient


【解决方案1】:

此错误是由于尝试以不受支持的方式组合 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

我不认为您包含的代码示例是这样做的

【讨论】:

  • 我该怎么办?导致最多 4 次 ping 它工作正常,但随后它向我显示错误!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-26
  • 2017-08-31
  • 2012-11-19
  • 2015-12-10
  • 2014-08-03
  • 2017-08-24
  • 2015-12-23
相关资源
最近更新 更多