5

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()

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.

Any suggestion will be greatly appreciated

asked Oct 16, 2012 at 1:14
1

2 Answers 2

5

The socket module has a timeout class. Your socket object, mysocket (of type socket.socket), does not have a timeout attribute.

Try this:

except timeout:
 print 'Request timed out!'
 mySocket.close()

Note that you should also be careful about using import * in this manner.

answered Oct 16, 2012 at 2:13
Sign up to request clarification or add additional context in comments.

Comments

1

I have python 2.7 and it works for me on ipython

Launching python -O
Python 2.7.2 (default, Apr 17 2012, 22:01:25) 
Type "copyright", "credits" or "license" for more information.
IPython 0.13 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from socket import *
In [2]: mySocket = socket(AF_INET, SOCK_DGRAM)
In [3]: mySocket.
mySocket.accept mySocket.dup mySocket.getsockopt mySocket.recv mySocket.sendall mySocket.shutdown
mySocket.bind mySocket.family mySocket.gettimeout mySocket.recv_into mySocket.sendto mySocket.type
mySocket.close mySocket.fileno mySocket.listen mySocket.recvfrom mySocket.setblocking 
mySocket.connect mySocket.getpeername mySocket.makefile mySocket.recvfrom_into mySocket.setsockopt 
mySocket.connect_ex mySocket.getsockname mySocket.proto mySocket.send mySocket.settimeout 
In [3]: mySocket.
mySocket.accept mySocket.dup mySocket.getsockopt mySocket.recv mySocket.sendall mySocket.shutdown
mySocket.bind mySocket.family mySocket.gettimeout mySocket.recv_into mySocket.sendto mySocket.type
mySocket.close mySocket.fileno mySocket.listen mySocket.recvfrom mySocket.setblocking 
mySocket.connect mySocket.getpeername mySocket.makefile mySocket.recvfrom_into mySocket.setsockopt 
mySocket.connect_ex mySocket.getsockname mySocket.proto mySocket.send mySocket.settimeout 
In [3]: mySocket.set
mySocket.setblocking mySocket.setsockopt mySocket.settimeout 
In [3]: mySocket.set
mySocket.setblocking mySocket.setsockopt mySocket.settimeout 
In [3]: mySocket.settimeout(1.0)
In [4]: 
answered Oct 16, 2012 at 1:33

1 Comment

Thank you ronak for your input. Do you have any suggestions why it does not work for me?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.