evg
evg

Reputation: 523

asyncore timeout

Simple async http client, hangs on long time with not available sites.

For example on site www.evtur.ru it waits for a long time, ten minutes or more.

I can't find way how to minimize timeout, is it possible to do timeout in 5 seconds for example?

# coding=utf-8
import asyncore
import string, socket
import StringIO
import mimetools, urlparse

class AsyncHTTP(asyncore.dispatcher):
    # HTTP requestor

    def __init__(self, uri):
        asyncore.dispatcher.__init__(self)

        self.uri = uri


        # turn the uri into a valid request
        scheme, host, path, params, query, fragment = urlparse.urlparse(uri)
        assert scheme == "http", "only supports HTTP requests"
        try:
            host, port = string.split(host, ":", 1)
            port = int(port)
        except (TypeError, ValueError):
            port = 80 # default port
        if not path:
            path = "/"
        if params:
            path = path + ";" + params
        if query:
            path = path + "?" + query

        self.request = "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host)

        self.host = host
        self.port = port

        self.status = None
        self.header = None
        self.http_code = None
        self.data = ""

        # get things going!
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        #self.connect((host, port))
        #return
        try:
            self.connect((host, port))
        except Exception,e:
            self.close()
            self.handle_connect_expt(e)

    def handle_connect(self):
        self.send(self.request)

    def handle_expt(self):
        print "handle_expt error!"
        self.close()

    def handle_error(self):
        print "handle_error error!"
        self.close()

    def handle_connect_expt(self,expt):
        print "connection error:",expt

    def handle_code(self):        
        print self.host," : ","recv http code: ",self.http_code


    def handle_read(self):
        data = self.recv(2048)
        #print data
        if not self.header:
            self.data = self.data + data
            try:
                i = string.index(self.data, "\r\n\r\n")
            except ValueError:
                return # continue
            else:
                # parse header
                fp = StringIO.StringIO(self.data[:i+4])
                # status line is "HTTP/version status message"
                status = fp.readline()
                self.status = string.split(status, " ", 2)
                self.http_code = self.status[1]
                self.handle_code()      

                # followed by a rfc822-style message header
                self.header = mimetools.Message(fp)
                # followed by a newline, and the payload (if any)
                data = self.data[i+4:]
                self.data = ""
                #header recived
                #self.close()


    def handle_close(self):
        self.close()



c = AsyncHTTP('http://www.python.org')
c = AsyncHTTP('http://www.evtur.ru')
asyncore.loop(timeout=0.05)

Upvotes: 4

Views: 4072

Answers (2)

Nick
Nick

Reputation: 10539

self.socket.settimeout(nn)

this not seems to work for me.

However, asyncore.loop() have parameter called count. here is pseudo-code what asyncore.loop() doing:

for i in range(count):
    ...
    select(... , timeout)
    ...

So if you want 5 seconds, you will need to do:

asyncore.loop(timeout=1, count=5)

But do not recommend working in this exact way. Note that if there are "events", you may have more than 5 "counts". I use following code:

start = int(time.time())

while True:
    asyncore.loop(timeout=1, count=5)
    print "LOOP : %d enqueued, waiting to finish" % len(asyncore.socket_map)

    if len(asyncore.socket_map) == 0 :
        break

    if int(time.time()) - start > timeout :
        print "LOOP : Timeout"
        break

Upvotes: 1

Fredrik Håård
Fredrik Håård

Reputation: 2915

You will have to set a timeout on the dispatcher socket. After calling

self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

you can call

self.socket.settimeout(nn)

to set a timeout on the socket. You can also set any other socket options as with any other socket.

Upvotes: 0

Related Questions