How to auto reconnect during IOError in python - python

I'm doing fetch links using python. and suddenly I lost the connection and display an error as below.
IOError: [Errno socket error] [Errno 110] Connection timed out
how to reconnect with the same link?
for instance
import urllib
a = 'http://anzaholyman.files.wordpress.com/2011/12/zip-it.gif'
image = urllib.URLopener()
image.retrieve(a,'1.jpg')

You can simply use the try..except syntax:
import urllib
a = 'http://anzaholyman.files.wordpress.com/2011/12/zip-it.gif'
image = urllib.URLopener()
while True:
try:
image.retrieve(a,'1.jpg')
break
except IOError:
pass

If there is an actual problem loading your figure, then, a simple while loop will never end and your application will seem to hang.
To prevent this, I usually use a counter:
tries = 5
while tries:
try:
image.retrieve(a,'1.jpg')
break
except IOError:
tries -= 1 #and maybe with 0.1-1 second rest here
else:
warn_or_raise_something
In order to prevent trasient problems, I also use sometimes a delay (time.sleep) between succesive tries after failed calls

Related

Is there a way to prevent the "bad connection" or "no network" error inside python?

I use urlopen command from urllib.request package and it works properly.
But because it is inside an infinite loop I want to consider the possible "No network" Conditions. I do not want my code to break down because of this error.
I tried the function below but it does not work:
def update():
try:
cmd = 'getUpdates'
resp = urlopen(URL + cmd)
line = aux_dec2utf8(resp)
upds = json.loads(line)
NoM = len(upds['result'])
except ValueError:
print('NO NETWORK')
return NoM, upds
Error Image
I think the issue here is the ValueError exception. You may want to set up an exception for urllib.HTTPError and/or urllib.URLError to catch the error you’re attempting to keep from breaking your script.
ValueError is used to catch an invalid argument and may not be what’s killing the flow.

Python and Urllib. How to handle the network exceptions?

I'm working on a simple code that is downloading a file over HTTP using the package urllib and urllib.request. Everything is working good excepted that I would like to be able to handle the network problems that could happens.
Checking if the computer is online (Connected to the internet). And proceed only if true.
Restarting the download of the file if during it, the connection is lost or too bad.
I would like, if possible, to use as less packages as possible.
Here is my actual code :
import urllib
import urllib.request
url = "http://my.site.com/myFile"
urlSplited = url.split('/')[-1];
print ("Downloading : "+urlSplited)
urllib.request.urlretrieve(url, urlSplited)
To check if a connection is etablished, I believe I can do something like
while connection() is true:
Download()
But that would do the downloading many times..
I'm working on Linux.
I suggest you to use a combination of try, while and sleep function. Like this:
import urllib
import urllib.request
import time
url = "http://my.site.com/myFile"
urlSplited = url.split('/')[-1];
try_again = True
print ("Downloading : "+urlSplited)
while try_again:
try:
urllib.request.urlretrieve(url, urlSplited, timeout = 100)
try_again = False
except Exception as e:
print(e)
time.sleep(600)
Replace while with if, then everything under it will run only once.
if connection() == True:
Download()
Also, connection() function could be something like this:
try:
urllib.urlopen(url, timeout=5)
return True
return False

How to prevent python script from exiting when it hits [Errno 32] Broken pipe

I have a python script and I'm using a while loop to loop forever, my script looks like this:
#!/usr/bin/python
import socket,select,time,base64,os,sys,re,datetime
def on_outbounddata(self):
print "ON_OUTBOUNDDATA"
netdata = self.netdata
if netdata.find('HTTP/1.') ==0:
ms = re.search(r"\^s(\d+)\^", payload)
if ms:
print "Sleeping for " + ms.group(1) + "ms"
dec = int(ms.group(1)) / float(1000)
time.sleep(dec)
print self.request[self.s]
try:
self.channel_[self.s].send(self.request[self.s])
self.request[self.s]=''
except ValueError:
print "self.s is not in the list (on_outbounddata)"
pass
netdata='HTTP/1.1 200 Connection established\r\n\r\n'
try:
self.channel[self.s].send(netdata)
except Exception, e:
print e
def main_loop(self):
while 1:
# Do stuff
self.on_outbounddata()
# Do stuff
if __name__ == '__main__':
server = TheServer('0.0.0.0', listen)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
The problem is that even though I have a while loop, sometimes the script will exit on its own when it encounters the following exception:
Traceback (most recent call last):
File "/usr/bin/socks", line 206, in <module>
server.main_loop()
File "/usr/bin/socks", line 121, in main_loop
self.on_outbounddata()
File "/usr/bin/socks", line 190, in on_outbounddata
self.channel_[self.s].send(self.request[self.s])
socket.error: [Errno 32] Broken pipe
I want my script to continue even though it excounters this exception socket.error: [Errno 32] Broken pipe. How can I accomplish this?
You did not provide a working example, so the answer can be only little bit theoretical.
Try to make exception handling to every point where exception can happen.
E.g.
while 1:
# Do stuff
self.on_outbounddata()
No exception handling here. Only partly inside of the function.
Also here you do not handle errors:
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
Here you only handle one type of exception:
try:
self.channel_[self.s].send(self.request[self.s])
self.request[self.s]=''
except ValueError:
print "self.s is not in the list (on_outbounddata)"
pass
You could use a blank except policy, but that is a bad practice typically. It would look like `
try:
self.channel[self.s].send(netdata)
except Exception, e:
print e
except:
code for blanket exception
` Check out How to handle a broken pipe (SIGPIPE) in python? it seems to be a very similar to your problem.

Detect what raises an exception

I made a Python 2.7.9 script which downloads some photos from the web using urllib.urlretrieve. I made a simple error dictation using the try and except commands, like so:
try:
urllib.urlretrieve("http://example.com/image.jpg", "1.jpg")
except IOError:
print "Could not connect to 'example.com'!"
However, I realized that IOError could be also raised when no space left on the the harddrive. I want to detect what cause the raise of the IOError (could not connect to example.com/no space left), and display the correct error message.
How can I do so? Thanks!
As zoosuck said, you could differentiate between errors with more than one except-Statement. If you want to differentiate between IOErrors, take a look at the documentation of IOError. It reveals that IOError has an attribute "errno". You can use the errno module (https://docs.python.org/2/library/errno.html) to identify the cause:
import urllib
import errno
try:
urllib.urlretrieve("http://example.com/image.jpg", "1.jpg")
except IOError, e:
if e.errno == errno.ENOSPC:
print "No space left on device"
else:
print "Could not connect to 'example.com'!"

Python else issues making an FTP program

I am having an issue with the else statement of this program... I have checked my spacing and it seems to be correct. I keep getting syntax error on the else statement. The program creates and file then attempts to upload it to a ftp server but if it fails to not say anything to the user and just continue It will try again when the program loops. Any help you could provide would be greatly appreciated.
#IMPORTS
import ConfigParser
import os
import random
import ftplib
from ftplib import FTP
#LOOP PART 1
from time import sleep
while True:
#READ THE CONFIG FILE SETUP.INI
config = ConfigParser.ConfigParser()
config.readfp(open(r'setup.ini'))
path = config.get('config', 'path')
name = config.get('config', 'name')
#CREATE THE KEYFILE
filepath = os.path.join((path), (name))
if not os.path.exists((path)):
os.makedirs((path))
file = open(filepath,'w')
file.write('text here')
file.close()
#Create Full Path
fullpath = path + name
#Random Sleep to Accomidate FTP Server
sleeptimer = random.randrange(1,30+1)
sleep((sleeptimer))
#Upload File to FTP Server
try:
host = '0.0.0.0'
port = 3700
ftp = FTP()
ftp.connect(host, port)
ftp.login('user', 'pass')
file = open(fullpath, "rb")
ftp.cwd('/')
ftp.storbinary('STOR ' + name, file)
ftp.quit()
file.close()
else:
print 'Something is Wrong'
#LOOP PART 2
sleep(180.00)
else is valid as part of an exception block, but it is only run if an exception is not raised and there must be a except defined before it.
(edit) Most people skip the else clause and just write code after exiting (dedenting) from the try/except clauses.
The quick tutorial is:
try:
# some statements that are executed until an exception is raised
...
except SomeExceptionType, e:
# if some type of exception is raised
...
except SomeOtherExceptionType, e:
# if another type of exception is raised
...
except Exception, e:
# if *any* exception is raised - but this is usually evil because it hides
# programming errors as well as the errors you want to handle. You can get
# a feel for what went wrong with:
traceback.print_exc()
...
else:
# if no exception is raised
...
finally:
# run regardless of whether exception was raised
...

Categories

Resources