timeout on on socket using alarm signal??

I

Ishwar Rattan

System Mandrake-9.1 Linux with Python-2.2.3

Tyring to time out from a non respnding server using SOCK_STREAM

Source is shown below

The program quits after first receipt of alram signal with messge

socket.error(4, 'Interrupted system call')

is there a way to retry the send and receive process?

-ishwar
----
import signal, sys
from socket import *
aTry = 0

def onalarm(signum, stackframe):
global aTry
if signum == signal.SIGALRM:
aTry = aTry + 1
print 'Alram seen..', aTry

signal.signal(signal.SIGALRM, onalarm)

def main():
global aTry
serverHost = 'localhost'
serverPort = 6007
mesg = 'hello there'

sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.connect((serverHost, serverPort))

while aTry < 5:
sockobj.send(mesg)
signal.alarm(1)
data = sockobj.recv(1024)
if data:
print 'Got: ', data
sockobj.close()
break
else:
aTry = aTry + 1
print 'No reply from server..'
sockobj.close()

if __name__ == '__main__':
main()
----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top