Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97107 > unrolled thread

Re: ConnectionError handling problem

Started byCameron Simpson <cs@zip.com.au>
First post2015-09-25 09:25 +1000
Last post2015-09-30 23:56 +0200
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 09:25 +1000
    Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 20:57 -0700
      Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 14:59 +1000
        Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:46 -0700
          Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 17:24 +1000
            Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-29 23:04 -0700
              Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-10-01 07:30 +1000
              Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-30 23:56 +0200

#97107 — Re: ConnectionError handling problem

FromCameron Simpson <cs@zip.com.au>
Date2015-09-25 09:25 +1000
SubjectRe: ConnectionError handling problem
Message-ID<mailman.157.1443140162.28679.python-list@python.org>
On 24Sep2015 12:38, Laura Creighton <lac@openend.se> wrote:
>In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes:
>>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.
[... snip ...]

Since nobody has offered this advice, let me:

Firstly, as already remarked bare excepts are overkill - they catch all sorts 
of things you shouldn't be handling.

That said, if you _don't know_ what exceptions are going to boils out of new 
code, this is one way to see them all. However, you are catching _everything_ 
but _not_ finding out what happened.

Try this:

    try:
        r=requests.post(url, data=query_args)
    except Exception as e:
        print "Exception:", e

That will at least _tell_ you what happened. You code is blithely presuming 
Connection Error and telling you that, but it is usually a lie.

Once you have characterised what exceptions you get, and which particular ones 
to handle, then modify your code to catch _only_ those and perform the specific 
suitable actions, and let the rest escape.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [next] | [standalone]


#97113

Fromshiva upreti <katewinslet626@gmail.com>
Date2015-09-24 20:57 -0700
Message-ID<4852c463-0c47-4fef-956a-c6c20f59fc0e@googlegroups.com>
In reply to#97107
Thank you Cameron.
I think the problem with my code is that it just hangs without raising any exceptions. And as mentioned by Laura above that when I press CTRL+C, it just catches that exception and prints ConnectionError which is definitely a lie in this case as you mentioned.
As my code doesnt raise any exception by itself, instead just hangs, I wont be able to characterize the exceptions and handle them individually.

[toc] | [prev] | [next] | [standalone]


#97114

FromCameron Simpson <cs@zip.com.au>
Date2015-09-25 14:59 +1000
Message-ID<mailman.160.1443158719.28679.python-list@python.org>
In reply to#97113
On 24Sep2015 20:57, shiva upreti <katewinslet626@gmail.com> wrote:
>Thank you Cameron.
>I think the problem with my code is that it just hangs without raising any exceptions. And as mentioned by Laura above that when I press CTRL+C, it just catches that exception and prints ConnectionError which is definitely a lie in this case as you mentioned.

Update it to report the _actual_ exception received. (If any.) At least then 
you will be sure.

>As my code doesnt raise any exception by itself, instead just hangs,

So, no "ConnectionError" message?

>I wont be able to characterize the exceptions and handle them individually.

Can you repost you code once it is modified to display the precise exception.

Note that one easy way to catch all but progressively handle specific 
exceptions looks like this:

  try:
     ... code ...
  except OSError as e:
    print OSError received, specifics are:", e
  except Exception as e:
    print "unhandled exception:", e
    break

and so on, inserting the exception types _above_ the final "except" as you add 
actions for them.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [next] | [standalone]


#97117

Fromshiva upreti <katewinslet626@gmail.com>
Date2015-09-24 22:46 -0700
Message-ID<550e9854-1247-4f6b-b9c7-7c8d7ea6ae37@googlegroups.com>
In reply to#97114
On Friday, September 25, 2015 at 10:55:45 AM UTC+5:30, Cameron Simpson wrote:
> On 24Sep2015 20:57, shiva upreti <katewinslet626@gmail.com> wrote:
> >Thank you Cameron.
> >I think the problem with my code is that it just hangs without raising any exceptions. And as mentioned by Laura above that when I press CTRL+C, it just catches that exception and prints ConnectionError which is definitely a lie in this case as you mentioned.
> 
> Update it to report the _actual_ exception received. (If any.) At least then 
> you will be sure.
> 
> >As my code doesnt raise any exception by itself, instead just hangs,
> 
> So, no "ConnectionError" message?
> 
> >I wont be able to characterize the exceptions and handle them individually.
> 
> Can you repost you code once it is modified to display the precise exception.
> 
> Note that one easy way to catch all but progressively handle specific 
> exceptions looks like this:
> 
>   try:
>      ... code ...
>   except OSError as e:
>     print OSError received, specifics are:", e
>   except Exception as e:
>     print "unhandled exception:", e
>     break
> 
> and so on, inserting the exception types _above_ the final "except" as you add 
> actions for them.
> 
> Cheers,
> Cameron Simpson <cs@zip.com.au>

Hi Cameron.
I think you may have misunderstood my problem.
When my code runs it doesnt throw any exception. It just hangs(stops executing) for unknown reasons. I want to know why. It doesnt even throw ConnectionError unless I press CTRL+C(only because I hard coded ConnectionError).
I can repost the whole problem again if you want me to.
Thanks.:)

[toc] | [prev] | [next] | [standalone]


#97120

FromCameron Simpson <cs@zip.com.au>
Date2015-09-25 17:24 +1000
Message-ID<mailman.165.1443165885.28679.python-list@python.org>
In reply to#97117
On 24Sep2015 22:46, shiva upreti <katewinslet626@gmail.com> wrote:
>On Friday, September 25, 2015 at 10:55:45 AM UTC+5:30, Cameron Simpson wrote:
>> On 24Sep2015 20:57, shiva upreti <katewinslet626@gmail.com> wrote:
>> >Thank you Cameron.
>> >I think the problem with my code is that it just hangs without raising any 
>> >exceptions. And as mentioned by Laura above that when I press CTRL+C, it 
>> >just catches that exception and prints ConnectionError which is definitely 
>> >a lie in this case as you mentioned.

Ok. You original code says:

  try:
    r=requests.post(url, data=query_args)
  except:
    print "Connection error"

and presumably we think your code is hanging inside the requests.post call? You 
should probably try to verify that, because if it is elsewhere you need to 
figure out where (lots of print statements is a first start on that).

I would open two terminals. Run your program until it hangs in one.

While it is hung, examine the network status. I'll presume you're on a UNIX 
system of some kind, probably Linux? If not it may be harder (or just require 
someone other than me).

If it is hung in the .post call, quite possibly it has an established connecion 
to the target server - maybe that server is hanging.

The shell command:

  netstat -rn | fgrep 172.16.68.6 | fgrep 8090

will show every connection to your server hosting the URL 
"http://172.16.68.6:8090/login.xml". That will tell you if you have a 
connection (if you are the only person doing the connecting from your machine).

If you have the "lsof" program (possibly in /usr/sbin, so "/usr/sbin/lsof") you 
can also examine the state of your hung Python program. This:

  lsof -p 12345

will report on the open files and network connections of the process with pid 
12345. Adjust to suit: you can find your program's pid ("process id") with the 
"ps" command, or by backgrounding your program an issuing the "jobs" command, 
which should show the process id more directly.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [next] | [standalone]


#97237

Fromshiva upreti <katewinslet626@gmail.com>
Date2015-09-29 23:04 -0700
Message-ID<ff56fdf3-e40b-4ebf-8f4a-524f3ce61fbd@googlegroups.com>
In reply to#97120
On Friday, September 25, 2015 at 12:55:01 PM UTC+5:30, Cameron Simpson wrote:
> On 24Sep2015 22:46, shiva upreti <katewinslet626@gmail.com> wrote:
> >On Friday, September 25, 2015 at 10:55:45 AM UTC+5:30, Cameron Simpson wrote:
> >> On 24Sep2015 20:57, shiva upreti <katewinslet626@gmail.com> wrote:
> >> >Thank you Cameron.
> >> >I think the problem with my code is that it just hangs without raising any 
> >> >exceptions. And as mentioned by Laura above that when I press CTRL+C, it 
> >> >just catches that exception and prints ConnectionError which is definitely 
> >> >a lie in this case as you mentioned.
> 
> Ok. You original code says:
> 
>   try:
>     r=requests.post(url, data=query_args)
>   except:
>     print "Connection error"
> 
> and presumably we think your code is hanging inside the requests.post call? You 
> should probably try to verify that, because if it is elsewhere you need to 
> figure out where (lots of print statements is a first start on that).
> 
> I would open two terminals. Run your program until it hangs in one.
> 
> While it is hung, examine the network status. I'll presume you're on a UNIX 
> system of some kind, probably Linux? If not it may be harder (or just require 
> someone other than me).
> 
> If it is hung in the .post call, quite possibly it has an established connecion 
> to the target server - maybe that server is hanging.
> 
> The shell command:
> 
>   netstat -rn | fgrep 172.16.68.6 | fgrep 8090
> 
> will show every connection to your server hosting the URL 
> "http://172.16.68.6:8090/login.xml". That will tell you if you have a 
> connection (if you are the only person doing the connecting from your machine).
> 
> If you have the "lsof" program (possibly in /usr/sbin, so "/usr/sbin/lsof") you 
> can also examine the state of your hung Python program. This:
> 
>   lsof -p 12345
> 
> will report on the open files and network connections of the process with pid 
> 12345. Adjust to suit: you can find your program's pid ("process id") with the 
> "ps" command, or by backgrounding your program an issuing the "jobs" command, 
> which should show the process id more directly.
> 
> Cheers,
> Cameron Simpson <cs@zip.com.au>

Hi Cameron.
Yes I use ubuntu 14.04. I will try what you suggested. But I cant understand one thing, for whatever reason the script is hanging, why does it resumes almost instantaneously when I press CTRL+C.
Thanks.

[toc] | [prev] | [next] | [standalone]


#97258

FromCameron Simpson <cs@zip.com.au>
Date2015-10-01 07:30 +1000
Message-ID<mailman.278.1443649730.28679.python-list@python.org>
In reply to#97237
On 29Sep2015 23:04, shiva upreti <katewinslet626@gmail.com> wrote:
>On Friday, September 25, 2015 at 12:55:01 PM UTC+5:30, Cameron Simpson wrote:
>> Ok. You original code says:
>>
>>   try:
>>     r=requests.post(url, data=query_args)
>>   except:
>>     print "Connection error"
>>
>> and presumably we think your code is hanging inside the requests.post call? You
>> should probably try to verify that, because if it is elsewhere you need to
>> figure out where (lots of print statements is a first start on that).
>> I would open two terminals. Run your program until it hangs in one.
[...various things to do to check _exactly_ where the hang is occurring...]
>
>Yes I use ubuntu 14.04. I will try what you suggested. But I cant understand 
>one thing, for whatever reason the script is hanging, why does it resumes 
>almost instantaneously when I press CTRL+C.

Most likely the Ctrl-C interrupts whatever system call is hanging, causing it 
to return (failed, probably with errno EINTR). And the python program resumes 
because the OS system call has returned.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [next] | [standalone]


#97259

FromLaura Creighton <lac@openend.se>
Date2015-09-30 23:56 +0200
Message-ID<mailman.279.1443650168.28679.python-list@python.org>
In reply to#97237
In a message of Thu, 01 Oct 2015 07:30:59 +1000, Cameron Simpson writes:
>Most likely the Ctrl-C interrupts whatever system call is hanging, causing it 
>to return (failed, probably with errno EINTR). And the python program resumes 
>because the OS system call has returned.
>
>Cheers,
>Cameron Simpson <cs@zip.com.au>

Shiva Upreti needs to post the current code, but last time I read it
the problem was that control-C is a KeyboardInterrupt, which the
program was catching, and then continuing the loop.

Laura

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web