1

when I use urllib2 and sometimes get an error like "urllib2 urlopen error [Errno 61] Connection refused", I will try to connect again. However, once the connection gets error, it will never succeed until I restart the python script. The following code is connection part.

    req = urllib2.Request(self.__servAddr, body)
    response = None
    try:
        response = urllib2.urlopen(req, timeout = 15)
        res = response.read()
        res = eval(res)
        print res
        if 200 == res["retcode"]:
            if res["data"]:
                infoReady = 1
                xxxxxx
    except Exception, e:
        print "request error %s" % str(e) 
        if response:
            response.close()

It semems the current connection has an effct on the next connection. How can I fix?

Yating Dan
  • 11
  • 1
  • 5
  • What are you connecting to? The remote host is likely is refusing your connection after x many attempts... – l'L'l Jan 12 '17 at 02:46
  • The remote host is just a normal server. So you are saying maybe it's the server's problem? @l'L'l – Yating Dan Jan 12 '17 at 02:54
  • Generally if a connection is "refused" it's whatever it is you were trying to connect pushing the buttons. If you are hammering away at servers, scraping, etc. they often do this. Generally you'll need to use proper headers or their API (if they have one available) in order to resolve the issue. Trying to the headers approach is generally the first place to start: http://stackoverflow.com/a/36853786/499581 – l'L'l Jan 12 '17 at 02:56

1 Answers1

0

When the connection is refused this often times is a proxy issue. If you are behind a corporate proxy it is probably being blocked. You can get arround it by setting the following environment variable:

import os
os.environ["HTTPS_PROXY"] = "http://username:password@your-corporate-proxy.com:port"

NOTE: not all proxies require the username and password. in which case this may be omitted. also you may substitute HTTPS_PROXY with HTTP_PROXY if that is the type of proxy you are subscribed to.