ConnectionError handling problem

Mark Lawrence breamoreboy at
Sat Sep 19 18:18:12 CEST 2015

On 19/09/2015 07:13, shiva upreti wrote:
> 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.

Never use a bare except in Python, always handle the bare minimum number 
of exceptions that you need to, in this case your ConnectionError.

My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

More information about the Python-list mailing list