问题描述
我在Python 2.7中编写了一个非常简单的udp套接字连接
I am writing a very simple udp socket connection in Python 2.7
服务器端已启动并正在运行.我在客户端遇到了麻烦.
The server side is up and running.I have trouble on the client side.
from socket import *
serverName = '127.0.0.1'
serverPort = 5444
counter = 1;
while counter < 55:
mySocket = socket(AF_INET,SOCK_DGRAM)
try:
mySocket.settimeout(1.0)
message = raw_input('')
mySocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = mySocket.recvfrom(1024)
except mySocket.timeout:
print 'Request timed out!'
mySocket.close()
else:
print 'Server Response: '
print modifiedMessage
mySocket.close()
我收到以下错误.除了mySocket.timeout:AttributeError:'_socketobject'对象没有属性'timeout'
I am getting the following error.except mySocket.timeout:AttributeError: '_socketobject' object has no attribute 'timeout'
我不明白为什么没有超时属性?!
I can't understand how come there is no timeout attribute?!
实际上,我正在查看智能,并且也没有这样的属性.
In fact I am looking at the intelisense and there is no such attribute too.
任何建议将不胜感激
推荐答案
socket
模块具有一个timeout
类.您的套接字对象mysocket
(类型为socket.socket
)没有timeout
属性.
The socket
module has a timeout
class. Your socket object, mysocket
(of type socket.socket
), does not have a timeout
attribute.
尝试一下:
except timeout:
print 'Request timed out!'
mySocket.close()
请注意,以这种方式使用import *
时也应小心.
Note that you should also be careful about using import *
in this manner.
这篇关于Python套接字超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!