4

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

nej
  • 43
  • 1
  • 1
  • 4
  • Check this thread http://stackoverflow.com/questions/3432102/python-socket-connection-timeout – ronak Oct 16 '12 at 01:28

2 Answers2

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.

John Lyon
  • 9,978
  • 2
  • 34
  • 42
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]: 
ronak
  • 1,492
  • 2
  • 17
  • 32
  • Thank you ronak for your input. Do you have any suggestions why it does not work for me? – nej Oct 16 '12 at 01:54