Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96843 > unrolled thread
| Started by | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| First post | 2015-09-18 23:13 -0700 |
| Last post | 2015-09-24 22:42 -0700 |
| Articles | 13 — 5 participants |
Back to article view | Back to comp.lang.python
ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-18 23:13 -0700
Re: ConnectionError handling problem Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-19 17:18 +0100
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 12:45 +0000
Re: ConnectionError handling problem Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-20 14:13 +0100
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 13:28 +0000
Re: ConnectionError handling problem Chris Angelico <rosuav@gmail.com> - 2015-09-20 23:25 +1000
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 13:33 +0000
Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-20 16:40 +0200
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-23 19:49 -0700
Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-24 12:38 +0200
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 20:52 -0700
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:41 -0700
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:42 -0700
| From | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| Date | 2015-09-18 23:13 -0700 |
| Subject | ConnectionError handling problem |
| Message-ID | <c56e680b-2e04-4fc6-ba57-5ec64459bce9@googlegroups.com> |
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="http://172.16.68.6:8090/login.xml"
query_args = {'mode':'191', 'username':str(i), 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
try:
r=requests.post(url, 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.
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-09-19 17:18 +0100 |
| Message-ID | <mailman.24.1442679607.21674.python-list@python.org> |
| In reply to | #96843 |
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="http://172.16.68.6:8090/login.xml"
> query_args = {'mode':'191', 'username':str(i), 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
>
> try:
> r=requests.post(url, 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
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2015-09-20 12:45 +0000 |
| Message-ID | <slrnmvtanf.48q.jon+usenet@frosty.unequivocal.co.uk> |
| In reply to | #96856 |
On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > On 19/09/2015 07:13, shiva upreti wrote: >> try: >> r=requests.post(url, data=query_args) >> except: >> print "Connection error" > > Never use a bare except in Python, always handle the bare minimum number > of exceptions that you need to, in this case your ConnectionError. While I entirely agree with the principle of being specific in what exceptions you are catching (with the absolute maximum being 'Exception'), it is often not obvious which ones you need to specify. The code above probably actually needs to catch EnvironmentError if it is intended to intercept all network problems.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-09-20 14:13 +0100 |
| Message-ID | <mailman.36.1442754847.21674.python-list@python.org> |
| In reply to | #96873 |
On 20/09/2015 13:45, Jon Ribbens wrote: > On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >> On 19/09/2015 07:13, shiva upreti wrote: >>> try: >>> r=requests.post(url, data=query_args) >>> except: >>> print "Connection error" >> >> Never use a bare except in Python, always handle the bare minimum number >> of exceptions that you need to, in this case your ConnectionError. > > While I entirely agree with the principle of being specific in what > exceptions you are catching (with the absolute maximum being > 'Exception'), it is often not obvious which ones you need to specify. > The code above probably actually needs to catch EnvironmentError if > it is intended to intercept all network problems. > I doubt it, as from the docs "The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of OSError.". EnvironmentError is one of those listed. I'd have thought it far more likely that you'd want to catch one or more of the OSError subclasses, as finer grained exceptions was part of the rationale behind PEP 3151 -- Reworking the OS and IO exception hierarchy. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2015-09-20 13:28 +0000 |
| Message-ID | <slrnmvtd86.48q.jon+usenet@frosty.unequivocal.co.uk> |
| In reply to | #96874 |
On 2015-09-20, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > On 20/09/2015 13:45, Jon Ribbens wrote: >> On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >>> On 19/09/2015 07:13, shiva upreti wrote: >>>> try: >>>> r=requests.post(url, data=query_args) >>>> except: >>>> print "Connection error" >>> >>> Never use a bare except in Python, always handle the bare minimum number >>> of exceptions that you need to, in this case your ConnectionError. >> >> While I entirely agree with the principle of being specific in what >> exceptions you are catching (with the absolute maximum being >> 'Exception'), it is often not obvious which ones you need to specify. >> The code above probably actually needs to catch EnvironmentError if >> it is intended to intercept all network problems. > > I doubt it, as from the docs "The following exceptions are kept for > compatibility with previous versions; starting from Python 3.3, they are > aliases of OSError.". EnvironmentError is one of those listed. I'd > have thought it far more likely that you'd want to catch one or more of > the OSError subclasses, as finer grained exceptions was part of the > rationale behind PEP 3151 -- Reworking the OS and IO exception hierarchy. PEP 3151 has nothing to do with it, as the code in question in this thread is running under Python 2 not Python 3. If you catch only OSError then you will miss almost all the likely exceptions raised by requests.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-09-20 23:25 +1000 |
| Message-ID | <mailman.38.1442755567.21674.python-list@python.org> |
| In reply to | #96873 |
On Sun, Sep 20, 2015 at 10:45 PM, Jon Ribbens <jon+usenet@unequivocal.co.uk> wrote: > On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >> On 19/09/2015 07:13, shiva upreti wrote: >>> try: >>> r=requests.post(url, data=query_args) >>> except: >>> print "Connection error" >> >> Never use a bare except in Python, always handle the bare minimum number >> of exceptions that you need to, in this case your ConnectionError. > > While I entirely agree with the principle of being specific in what > exceptions you are catching (with the absolute maximum being > 'Exception'), it is often not obvious which ones you need to specify. > The code above probably actually needs to catch EnvironmentError if > it is intended to intercept all network problems. General principle: If you don't know what you should be catching, _catch nothing_. Anything that happens will get printed to the console. Then when you find that something's getting thrown, you check out what its name is, and maybe what its superclasses are (in case there's a broader one worth catching), and only THEN do you stick in a try/except. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2015-09-20 13:33 +0000 |
| Message-ID | <slrnmvtdg6.48q.jon+usenet@frosty.unequivocal.co.uk> |
| In reply to | #96876 |
On 2015-09-20, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Sep 20, 2015 at 10:45 PM, Jon Ribbens ><jon+usenet@unequivocal.co.uk> wrote: >> On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >>> On 19/09/2015 07:13, shiva upreti wrote: >>>> try: >>>> r=requests.post(url, data=query_args) >>>> except: >>>> print "Connection error" >>> >>> Never use a bare except in Python, always handle the bare minimum number >>> of exceptions that you need to, in this case your ConnectionError. >> >> While I entirely agree with the principle of being specific in what >> exceptions you are catching (with the absolute maximum being >> 'Exception'), it is often not obvious which ones you need to specify. >> The code above probably actually needs to catch EnvironmentError if >> it is intended to intercept all network problems. > > General principle: If you don't know what you should be catching, > _catch nothing_. Anything that happens will get printed to the > console. Then when you find that something's getting thrown, you check > out what its name is, and maybe what its superclasses are (in case > there's a broader one worth catching), and only THEN do you stick in a > try/except. I'm afraid I think that's absolutely terrible advice. I agree that you should not be afraid to let exceptions propagate up the stack (one of the things that's disastrously wrong about Java) but the case at hand of "try and fetch this network resource and then do something if it didn't succeed" is perfectly reasonable, and trying to code that by trial and error is a perfect example of bad programming.
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-20 16:40 +0200 |
| Message-ID | <mailman.39.1442760018.21674.python-list@python.org> |
| In reply to | #96843 |
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="http://172.16.68.6:8090/login.xml"
>> query_args = {'mode':'191', 'username':str(i), 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
>>
>> try:
>> r=requests.post(url, 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, http://172.16.68.6:8090/ 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'.
Laura
[toc] | [prev] | [next] | [standalone]
| From | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| Date | 2015-09-23 19:49 -0700 |
| Message-ID | <e13ed686-94af-444d-a67b-194fe13b0769@googlegroups.com> |
| In reply to | #96879 |
On Sunday, September 20, 2015 at 8:11:18 PM UTC+5:30, Laura Creighton wrote:
> 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="http://172.16.68.6:8090/login.xml"
> >> query_args = {'mode':'191', 'username':str(i), 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
> >>
> >> try:
> >> r=requests.post(url, 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, http://172.16.68.6:8090/ 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'.
>
> Laura
Hi
If my script hangs because of the reasons you mentioned above, why doesnt it catch ConnectionError?
My script stops for a while and when I press CTRL+C, it shows ConnectionError without terminating the process, and the script resumes from where it left off.
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-24 12:38 +0200 |
| Message-ID | <mailman.120.1443091118.28679.python-list@python.org> |
| In reply to | #97057 |
In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes: >Hi >If my script hangs because of the reasons you mentioned above, why doesnt it catch ConnectionError? >My script stops for a while and when I press CTRL+C, it shows ConnectionError without terminating the process, and the script resumes from where it left off. This is exactly what you asked it to do. :) >> try: >> r=requests.post(url, data=query_args) >> except: >> print "Connection error" >> time.sleep(30) >> continue try to do something until you get an Exception. Since that is a naked except, absolutely any Exception will do. That you print out 'Connection error' doesn't mean that you are only catching exceptions raised by trying to send something to the other end ... any Exception will do. So what happens when you press Control-C? You get a KeyboardInterrupt exception! :) see: https://docs.python.org/2/library/exceptions.html And you are catching it :) And when you catch it you print Connection error and keep on trying. This is why people were telling you that naked try:except: pairs, are rarely what you want. You didn't want to catch control-c but you caught it anyway. Laura
[toc] | [prev] | [next] | [standalone]
| From | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| Date | 2015-09-24 20:52 -0700 |
| Message-ID | <b5c1b4fa-1447-47ea-8308-63f27b454b70@googlegroups.com> |
| In reply to | #97068 |
Thank you. I didnt know about keyboard interrupt exception. It means my code hangs without raising any exceptions, what should i do in this case?
[toc] | [prev] | [next] | [standalone]
| From | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| Date | 2015-09-24 22:41 -0700 |
| Message-ID | <9768479d-6277-4ce1-9c5a-16cfbea5f100@googlegroups.com> |
| In reply to | #97068 |
On Thursday, September 24, 2015 at 4:09:04 PM UTC+5:30, Laura Creighton wrote: > In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes: > >Hi > >If my script hangs because of the reasons you mentioned above, why doesnt it catch ConnectionError? > >My script stops for a while and when I press CTRL+C, it shows ConnectionError without terminating the process, and the script resumes from where it left off. > > This is exactly what you asked it to do. :) > > >> try: > >> r=requests.post(url, data=query_args) > >> except: > >> print "Connection error" > >> time.sleep(30) > >> continue > > try to do something until you get an Exception. Since that is a > naked except, absolutely any Exception will do. That you > print out 'Connection error' doesn't mean that you are only > catching exceptions raised by trying to send something to the > other end ... any Exception will do. > > So what happens when you press Control-C? > > You get a KeyboardInterrupt exception! :) > > see: https://docs.python.org/2/library/exceptions.html > > And you are catching it :) > And when you catch it you print Connection error and keep on > trying. > > This is why people were telling you that naked try:except: pairs, > are rarely what you want. You didn't want to catch control-c but > you caught it anyway. > > Laura Thank you. I didnt know about keyboard interrupt exception. It means my code hangs without raising any exceptions, what should i do in this case?
[toc] | [prev] | [next] | [standalone]
| From | shiva upreti <katewinslet626@gmail.com> |
|---|---|
| Date | 2015-09-24 22:42 -0700 |
| Message-ID | <ee24055f-3598-4621-bae3-23a5a68fe532@googlegroups.com> |
| In reply to | #97068 |
On Thursday, September 24, 2015 at 4:09:04 PM UTC+5:30, Laura Creighton wrote: > In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes: > >Hi > >If my script hangs because of the reasons you mentioned above, why doesnt it catch ConnectionError? > >My script stops for a while and when I press CTRL+C, it shows ConnectionError without terminating the process, and the script resumes from where it left off. > > This is exactly what you asked it to do. :) > > >> try: > >> r=requests.post(url, data=query_args) > >> except: > >> print "Connection error" > >> time.sleep(30) > >> continue > > try to do something until you get an Exception. Since that is a > naked except, absolutely any Exception will do. That you > print out 'Connection error' doesn't mean that you are only > catching exceptions raised by trying to send something to the > other end ... any Exception will do. > > So what happens when you press Control-C? > > You get a KeyboardInterrupt exception! :) > > see: https://docs.python.org/2/library/exceptions.html > > And you are catching it :) > And when you catch it you print Connection error and keep on > trying. > > This is why people were telling you that naked try:except: pairs, > are rarely what you want. You didn't want to catch control-c but > you caught it anyway. > > Laura Thank you. I didnt know about keyboard interrupt exception. It means my code hangs without raising any exceptions, what should i do in this case?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web