我正在设置一个简单的客户端套接字(我的服务器套接字工作正常)。但我被一个lil bug困住了。这是我的代码,这是错误。无法在网上找到此错误。
from socket import *
import sys
host=socket.gethostname()
#host=127.0.0.1
serverPort= 12345
clientSocket =socket(AF_INET,SOCK_STREAM)
clientSocket.connect((127.0.0.1,serverPort))
msg= raw_input("Input text here:")
clientSocket.send(msg)
modmsg= clientSocket.recv(1024)
print "from server", modmsg
clientSocket.close()
错误:
Traceback (most recent call last):
File "tcp_client.py", line 5, in <module>
clientSocket.connect((serverName,serverPort))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.gaierror: [Errno -5] No address associated with hostname
答案 0 :(得分:0)
你的代码在这里是不正确的(你发布了你运行的真实代码吗?):
connect
接受1个参数,它是一个元组from socket import *
,则无法执行socket.socket
固定:
import socket
import sys
host=socket.gethostname()
serverPort= 12345
clientSocket = socket.socket(AF_INET,SOCK_STREAM)
clientSocket.connect((host,serverPort))
msg= raw_input("Input text here:")
clientSocket.send(msg)
modmsg= clientSocket.recv(1024)
print "from server", modmsg
clientSocket.close()
现在,当我运行此代码(我没有客户端)而不是您的错误时,我得到了正确的超时。