Hi,
I'm using Python 2.3s timeout sockets and have code like this to read a page
from web:
request = ...
self.page = urllib2.urlopen (request)
and later:
try:
self.data = self.page.read( )
except socket.error,e: ...
except socket.timeout: ...
except timeout: ...
but none of these excepts catches the the timeout while reading the data. I
still get the following exception, which I cannot handle:
....
File "F:\CrawlingFra mework\Rules\To ols\__init__.py ", line 91, in __init__
self.data = self.page.read( )
File "C:\Python23\li b\socket.py", line 283, in read
data = self._sock.recv (recv_size)
timeout: timed out
Any hint on how to handle this exception or what's going wrong?
regards,
Achim 17 52224
Achim Domma wrote: I'm using Python 2.3s timeout sockets and have code like this to read a page from web:
request = ... self.page = urllib2.urlopen (request)
and later:
try: self.data = self.page.read( ) except socket.error,e: ... except socket.timeout: ... except timeout: ...
but none of these excepts catches the the timeout while reading the data. I still get the following exception, which I cannot handle:
socket.timeout is a subclass of socket.error, so the timeout exception
should be caught by the first except clause.
However, I could reproduce your uncaught exception with the following
minimalist code:
from urllib2 import urlopen
import socket
slowurl = "http://127.0.0.1/timeout?delay=1 00"
socket.setdefau lttimeout(1)
data = urlopen(slowurl )
try:
data.read()
except: # should catch ANY exception
print "Timeout raised and caught" # this never shows
So it seems there is *no* way to catch the error.
I think you should file a bug report.
Peter
"Peter Otten" <__*******@web. de> wrote in message
news:bk******** *****@news.t-online.com... So it seems there is *no* way to catch the error. I think you should file a bug report.
Where/How do I do that?
Achim
Peter Otten wrote: socket.timeout is a subclass of socket.error, so the timeout exception should be caught by the first except clause.
So it seems there is *no* way to catch the error. I think you should file a bug report.
Hmmm.
What is wrong with the following code? It seems to do what you need:
#============== =============== =============== ===========
from urllib2 import urlopen
import socket
import sys
slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wai t=10"
socket.setdefau lttimeout(1)
try:
data = urlopen(slowurl )
data.read()
except socket.error:
errno, errstr = sys.exc_info()[:2]
if errno == socket.timeout:
print "There was a timeout"
else:
print "There was some other socket error"
#============== =============== =============== ==============
regards,
--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Achim Domma wrote: So it seems there is *no* way to catch the error. I think you should file a bug report.
Where/How do I do that? http://www.python.org/dev has an outside link to Bug Tracker leading to a
Sourceforge page where you can submit a short description of the bug.
Have a look at the list of bugs both to see if your bug has already been
submitted by others as well as for example submissions.
Peter
Alan Kennedy wrote: Hmmm.
What is wrong with the following code? It seems to do what you need:
#============== =============== =============== =========== from urllib2 import urlopen import socket import sys
slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wai t=10" socket.setdefau lttimeout(1)
try: data = urlopen(slowurl ) data.read() except socket.error: errno, errstr = sys.exc_info()[:2] if errno == socket.timeout: print "There was a timeout" else: print "There was some other socket error" #============== =============== =============== ==============
You are right. I did not read the traceback carefully.
Peter
"Achim Domma" <do***@procoder s.net> writes: I'm using Python 2.3s timeout sockets and have code like this to read a page from web:
request = ... self.page = urllib2.urlopen (request)
and later:
try: self.data = self.page.read( ) except socket.error,e: ... except socket.timeout: ... except timeout: ...
As another poster pointed out, socket.timeout is a subclass of
socket.error. (This was so you could write code that treated all
socket errors alike if you wanted timeouts but didn't need to deal
with them separately.)
Section 7.4 of the Language Reference says:
[...] When an exception occurs in the try suite, a search for
an exception handler is started. This search inspects the
except clauses in turn until one is found that matches the
exception. [...]
So, all you need to do is put the socket.timeout except clause before
any socket.error clause:
try:
self.data = self.page.read( )
except socket.timeout: ...
except socket.error,e: ...
/Bob
"Bob Halley" <ha****@play-bow.org> wrote in message
news:ma******** *************** ***********@pyt hon.org... So, all you need to do is put the socket.timeout except clause before any socket.error clause:
try: self.data = self.page.read( ) except socket.timeout: ... except socket.error,e: ...
Thanks, that works fine, but I don't understand why the exception was not
catched in my case. If I write it like this
try:
self.data = self.page.read( )
except socket.error,e: ...
except socket.timeout: ...
the exception should be catched by the socket.error handler. Or am I wrong?
In my case it was not catched at all. Very mysterious from my point of view,
but it works now.
Achim
Achim Domma wrote: "Bob Halley" <ha****@play-bow.org> wrote in message news:ma******** *************** ***********@pyt hon.org... So, all you need to do is put the socket.timeout except clause before any socket.error clause:
try: self.data = self.page.read( ) except socket.timeout: ... except socket.error,e: ...
Thanks, that works fine, but I don't understand why the exception was not catched in my case. If I write it like this
try: self.data = self.page.read( ) except socket.error,e: ... except socket.timeout: ...
the exception should be catched by the socket.error handler. Or am I wrong? In my case it was not catched at all. Very mysterious from my point of view, but it works now.
Achim
Achim, both you and me got it wrong the first time. The important part is to
put both urlopen() and page.read() into the try clause, as Alan Kennedy has
already shown. Both statements can throw a timeout exception, so it's sheer
luck that it worked this time.
And yes, if you put
except socket.error:
before
except socket.timeout:
the latter will never be executed. General structure:
from urllib2 import urlopen
import socket
slowurl = "http://127.0.0.1/timeout?delay=1 00"
socket.setdefau lttimeout(1)
try:
data = urlopen(slowurl )
data.read()
except socket.timeout:
print "Timeout raised and caught"
Peter
What's the best way to do this under Python 2.1?
I believe socket.timeout was a 2.3 feature.
Wrap it in threads?
I'd like to do something similar for the Gibraltar
Linux firewall CD, but it only comes with Python 2.1.
Many thanks in advance.
-- Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tim Williams |
last post by:
(python newbie)
Is it possible to individually set the socket timeout of a connection
created by smtplib, rather than use the socket.setdefaulttimeout() value
used by the rest of the prog/script?
eg:
import smtplib
import (other stuff)
|
by: Paola |
last post by:
Hi,
I have a very strange problem with PowerTCP:
I made a wrapper between a server web and the browsers.
If the client ask Html pages or images there is no problem instead if
the client ask a pdf file or a zip file the response is "Object are
closed in the middle of the SSL handshake" and I receive half file.
Instead if I use another kind of procedure I receive a file too big
(e.g. original format 22 kb byte received 23 kb) the result of...
|
by: Franky |
last post by:
Hello,
How can I set timeout on the send for an asynchronous socket? I use the
setsocketoption's timeout but it does not seem work. The program is blocked
at the socket's EndSend step until the remote side closes the connection.
Thanks in advance,
Franky
|
by: Jaap Spies |
last post by:
Hi,
Running Fedora Core 4: Python 2.4.3 and Python 2.4.1.
I'm getting:
IOError: (2, 'No such file or directory')
all the time.
Trying to track down this problem:
Python 2.4.1 (#1, May 16 2005, 15:19:29)
|
by: Kiran |
last post by:
Hello All,
I am creating a socket connection in order to read and write to a
location. My problem is,the gui becomes unresponsive if the socket
times out.
I know that a good solution is to have the socket read and write with
a thread. However, I have tried this and have a problem where ONLY on
linux does the program crash, it DOES NOT crash on windows xp. I have
been trying to figure out why for about a week and have just given up....
| |
by: yawnmoth |
last post by:
Say I have the following script:
<?
$timeout = 5;
$start = time();
$fsock = fsockopen('125.1.119.10',80,$errno,$errstr,$timeout);
// reduce $timeout by the amount of time that it took for fsockopen to
connect.
$timeout-=(time()-$start);
|
by: Robin Becker |
last post by:
While messing about with some deliberate socket timeout code I got an unexpected
timeout after 20 seconds when my code was doing socket.setdefaulttimeout(120).
Closer inspection revealed that this error in fact seemed to come from the os
(in this case windows xp).
By inspection of test cases the error.reason from the deliberate socket timeout
looks like
'timed out'
whereas the windows caused timeout error.reason looks like
|
by: Heikki Toivonen |
last post by:
M2Crypto has some old code that gets and sets socket timeouts in
http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py,
for example:
def get_socket_read_timeout(self):
return
timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET,
socket.SO_RCVTIMEO, timeout.struct_size()))
The helper timeout module is here:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |