timeout on on socket using alarm signal??

Ishwar Rattan rattan at cps.cmich.edu
Thu Jul 22 20:33:41 CEST 2004


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



More information about the Python-list mailing list