ConnectionError handling problem

Laura Creighton lac at
Sun Sep 20 16:40:13 CEST 2015

The discussion about why or why not to use a bare except has gotten us
away from the problem reported, which is "why is my script hanging?"

In a message of Sat, 19 Sep 2015 17:18:12 +0100, Mark Lawrence writes:
>> I am learning python. I wrote a script using requests module.
>> The scripts runs fine for sometime, but after a while it hangs. When I press CTRL+C it shows ConnectionError even though I have included exception handling.
>> I am not sure as to why it cant handle ConnectionError when the script runs for a long time.
>> This is a part(causing issues) of the script I am running:
>> while(k<46656):
>> 		j=res[k]
>> 		url=""	
>> 		query_args = {'mode':'191', 'username':str(i), 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
>> 		try:
>>, data=query_args)
>> 		except:
>> 			print "Connection error"
>> 			time.sleep(30)
>> 			continue
>> 		html=r.text
>> 		if(len(html) < 10):
>> 			continue
>> 		if("The system could not log you on" not in html):
>> 			print "hello"
>> 			filehandle=open("ids", "a")
>> 			filehandle.write(str(i)+'\n')
>> 			filehandle.write(str(j)+'\n')
>> 			filehandle.close()
>> 			break
>> 		k=k+1
>> Any help will be highly appreciated.

So, when it hangs there are two main problems you can have.  One sort
is that the other side, it in itself
configured to only let people use a connection for a certain amount of
time.  If you are using it for longer, it will disconnect you.  Or the
other sort is that the other side disconnects people who have been
silent for a certain amount of time.  If you haven't send anything for
a certain amount of time it will log you off.  There are lots of other
things that work this way -- the system may see many attempts to login
and think you are trying to break into the system, the machine may
have crashed ... but the bottom line is that the reason your script
hangs is that there is nobody there on the other end.

The other sort of problem you can have is that the other end is
alive and well and talking to you, but you don't understand what
you are getting, and you are ignoring things you don't understand.
This can look exactly the same.

To find out what is going on you need to log what it is that you
are getting, to see if the answer is 'nothing' or 'garbage'.


More information about the Python-list mailing list