Python socket timeout exception

Python: Socket.timeout not handled by except

Some times I can effectively handle the socket.timeout, although some other times I get that socket timeout error and my script stops abruptly. Is there something I’m missing in my exception handling? how come it goes right trough it? Happens randomly in either one of the following pieces of code: First snippet:

for _ in range(max_retries): try: req = Request(url, headers=) response = urlopen(req,timeout=5) break except error.URLError as err: print("URL that generated the error code: ", url) print("Error description:",err.reason) except error.HTTPError as err: print("URL that generated the error code: ", url) print("Error code:", err.code) print("Error description:", err.reason) except socket.timeout: print("URL that generated the error code: ", url) print("Error description: No response.") except socket.error: print("URL that generated the error code: ", url) print("Error description: Socket error.") if response.getheader('Content-Type').startswith('text/html'): htmlBytes = response.read() htmlString = htmlBytes.decode("utf-8") self.feed(htmlString) 
for _ in range(max_retries): try: req = Request(i, headers=) with urlopen(req,timeout=5) as response, open(aux, 'wb') as out_file: shutil.copyfileobj(response, out_file) with open(path, fname), 'a') as f: f.write(("link" + str(intaux) + "-" + auxstr + str(index) + i[-4:] + " --- " + metadata[index%batch] + '\n')) break except error.URLError as err: print("URL that generated the error code: ", i) print("Error description:",err.reason) except error.HTTPError as err: print("URL that generated the error code: ", i) print("Error code:", err.code) print("Error description:", err.reason) except socket.timeout: print("URL that generated the error code: ", i) print("Error description: No response.") except socket.error: print("URL that generated the error code: ", i) print("Error description: Socket error.") 
Traceback (most recent call last): File "/mydir/crawler.py", line 202, in spider("urls.txt", maxPages=0, debug=1, dailyRequests=9600) File "/mydir/crawler.py", line 142, in spider parser.getLinks(url + "?start=" + str(currbot) + "&tab=" + auxstr,auxstr) File "/mydir/crawler.py", line 81, in getLinks htmlBytes = response.read() File "/usr/lib/python3.5/http/client.py", line 455, in read return self._readall_chunked() File "/usr/lib/python3.5/http/client.py", line 561, in _readall_chunked value.append(self._safe_read(chunk_left)) File "/usr/lib/python3.5/http/client.py", line 607, in _safe_read chunk = self.fp.read(min(amt, MAXAMOUNT)) File "/usr/lib/python3.5/socket.py", line 575, in readinto return self._sock.recv_into(b) File "/usr/lib/python3.5/ssl.py", line 929, in recv_into return self.read(nbytes, buffer) File "/usr/lib/python3.5/ssl.py", line 791, in read return self._sslobj.read(len, buffer) File "/usr/lib/python3.5/ssl.py", line 575, in read v = self._sslobj.read(len, buffer) socket.timeout: The read operation timed out 

EDIT: I noticed I missed a few lines of code thanks to @tdelaney I added them to the code above and I’m posting the solution I wrote if you post the solution or if you have a better approach to solve it I will mark the answer as correct Solution:

for _ in range(max_retries): try: req = Request(url, headers=) response = urlopen(req,timeout=5) break except error.URLError as err: print("URL that generated the error code: ", url) print("Error description:",err.reason) except error.HTTPError as err: print("URL that generated the error code: ", url) print("Error code:", err.code) print("Error description:", err.reason) except socket.timeout: print("URL that generated the error code: ", url) print("Error description: No response.") except socket.error: print("URL that generated the error code: ", url) print("Error description: Socket error.") if response.getheader('Content-Type').startswith('text/html'): for _ in range(max_retries): try: htmlBytes = response.read() htmlString = htmlBytes.decode("utf-8") self.feed(htmlString) break except error.URLError as err: print("URL that generated the error code: ", url) print("Error description:",err.reason) except error.HTTPError as err: print("URL that generated the error code: ", url) print("Error code:", err.code) print("Error description:", err.reason) except socket.timeout: print("URL that generated the error code: ", url) print("Error description: No response.") except socket.error: print("URL that generated the error code: ", url) print("Error description: Socket error.") 

Источник

Читайте также:  Javascript query select all

Handling a timeout error in Python sockets

The way I added the socket module was to import everything, but how do I handle exceptions? In the documentation it says you can use socket.timeouterror, but that doesn’t work for me. Also, how would I write the try exception block if I did import socket ? What is the difference in the imports?

4 Answers 4

adds all the names without leading underscores (or only the names defined in the modules __all__ attribute) in foo into your current module.

In the above code with from socket import * , you just want to catch timeout as you’ve pulled timeout into your current namespace.

from socket import * pulls in the definitions of everything inside of socket , but it doesn’t add socket itself.

try: # Socket stuff except timeout: print 'caught a timeout' 

Many people consider import * problematic and try to avoid it. This is because common variable names in two or more modules that are imported in this way will clobber one another.

For example, consider the following three Python files:

# File "a.py" def foo(): print "this is a's foo function" # File "b.py" def foo(): print "this is b's foo function" # File "yourcode.py" from a import * from b import * foo() 

If you run yourcode.py , you’ll see just the output «this is b’s foo function».

For this reason I’d suggest either importing the module and using it or importing specific names from the module:

For example, your code would look like this with explicit imports:

import socket from socket import AF_INET, SOCK_DGRAM def main(): client_socket = socket.socket(AF_INET, SOCK_DGRAM) client_socket.settimeout(1) server_host = 'localhost' server_port = 1234 while(True): client_socket.sendto('Message', (server_host, server_port)) try: reply, server_address_info = client_socket.recvfrom(1024) print reply except socket.timeout: # More code 

It is just a tiny bit more typing, but everything’s explicit and it’s pretty obvious to the reader where everything comes from.

Читайте также:  Построить график корреляции python

Источник

Catch socket timeout exception

I would like to catch the socket timeout (preferably in an exception) . except urllib.error.URLError: can catch it but I need to distinguished between a dead link and a timeout . If I take out the except urllib.error.URLError: the socket timeout does not catch and script terminates with an socket.timeout error

import urllib.request,urllib.parse,urllib.error import socket import http socket.setdefaulttimeout(0.1) try: file2 = urllib.request.Request('http://uk.geforce.com/html://') file2.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13") file3 = urllib.request.urlopen(file2).read().decode("utf8", 'ignore') except urllib.error.URLError: print('fail') except socket.error: print('fail') except socket.timeout: print('fail') except UnicodeEncodeError: print('fail') except http.client.BadStatusLine: print('fail') except http.client.IncompleteRead: print('fail') except urllib.error.HTTPError: print('fail') print('done') 

1 Answer 1

. except urllib.error.URLError, e: print type(e.reason) 

You will see whenever there is a socket timeout. Is this what you want?

try: data = urllib2.urlopen("http://www.abcnonexistingurlxyz.com") except Exception,e: print type(e.reason) .

I just realized that you tagged the question as Python-3.x. Unfortunately, I don’t have access to Python 3. The above code should work for 2.x where x>=3.

Источник

Python socket connection exception

I have a socket-connection going on and I wanna improve the exception handling and I’m stuck. Whenever I call socket.connect(server_address) with an invalid argument the program stops, but doesn’t seem to raise an exception. Here is my code:

import socket import sys import struct class ARToolkit(): def __init__(self): self.x = 0 self.y = 0 self.z = 0 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.logging = False def connect(self,server_address): try: self.sock.connect(server_address) except socket.error, msg: print "Couldnt connect with the socket-server: %s\n terminating program" % msg sys.exit(1) def initiate(self): self.sock.send("start_logging") def log(self): self.logging = True buf = self.sock.recv(6000) if len(buf)>0: nbuf = buf[len(buf)-12:len(buf)] self.x, self.y, self.z = struct.unpack(" 

The class maybe looks a bit wierd but its used for receiving coordinates from another computer running ARToolKit. Anyway, the issue is at the function connect() :

def connect(self,server_address): try: self.sock.connect(server_address) except socket.error, msg: print "Couldnt connect with the socket-server: %s\n terminating program" % msg sys.exit(1) 

If I call that function with a random IP-address and portnumber the whole program just stops up at the line:

self.sock.connect(server_address) 

The documentation I've read states that in case of an error it will throw a socket.error-exception. I've also tried with just:

This, if I'm not mistaken, will catch any exceptions, and still it yields no result. I would be very grateful for a helping hand. Also, is it okay to exit programs using sys.exit when an unwanted exception occurs? Thank you

Источник

Оцените статью