Question

How do I find out if a connection has been broken using the httplib library? Seems like something so basic yet I can't find the answer on here or google.

Was it helpful?

Solution

While Connecting You get one of these:

http://docs.python.org/library/httplib.html#httplib.HTTPException

you could do something like this.

>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> try:
>>>     conn.request("GET", "/index.html")
>>> except Exception as e:
>>>     #take action according to the error.
>>>     print(type(e))
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason

Example taken from www.python.org and edited

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top