Python requests exceptions timeout - python

I wrote a python script like this:
#!/usr/bin/python
import sys
import requests
if len(sys.argv) != 2:
print "Usage: python %s <IP-LIST>" % (sys.argv[0])
sys.exit();
InputFile = sys.argv[1]
try:
File = open(InputFile)
for ip in File:
IP = ip.rstrip()
out = requests.get("http://" + IP, timeout=5)
out.status_code
except (KeyboardInterrupt, SystemExit):
print "\nKeyboardInterruption with Ctrl+c signal"
except IOError as e:
print "The file \"%s\" does not exist!" % (sys.argv[1])
When url has nothing to respond the following output appears for me:
The file "list.txt" does not exist!
Why?

requests uses exceptions that subclass IOError, which you are catching and assuming to be file not found. If you were using Python 3 you could catch the more specific FileNotFoundError. Here you should put an except requests.RequestException block above your except IOError block (or break it out into specific requests errors if you want.

Related

python wait using a timeout for an external program to finish

Before I run my python script I need to check if an external program is running and wait for it to finish.
I have a few pieces but am having some difficulty putting it all together into a cohesive script.
I can use the following to get the PID of a program and a while loop which will timeout in 10 seconds
from subprocess import check_output
import time
def get_pid(name):
return check_output(["pidof",name])
print "%s" % get_pid("java")
timeout = time.time() + 10
test = 0
while True:
if time.time() > timeout:
print "exception goes here"
break
test = test + 1
time.sleep(1)
If the program is not running get_pid would fail and I think I need to catch the exception in that case? This is where I am not certain where to go. Maybe there is a better way to approach this?
==============
Here is an update which seems to work correctly.
from subprocess import check_output, CalledProcessError
import time
def get_pid(name):
try:
process_status = check_output(["pidof",name])
except CalledProcessError as e:
process_status = None
return process_status
timeout = time.time() + 10
program = "java"
while get_pid(program) != None:
time.sleep(1)
print "1 second loop"
if time.time() > timeout:
raise ValueError ('Timeout exceeded')
print "%s is not running" % program
You can try catching this exception. Because when check_output returns non zero exception instance of CalledProcessError is raised which can be handled. Like this.
from subprocess import CalledProcessError
def get_pid(name):
try:
process_status = check_output(["pidof",name])
except CalledProcessError as e:
process_status = None # You can additionally get return code in the returncode attribute and any output in the output attribute.
return process_status
Hope it works!
Have you tried using the try and except block?
E.g.
try:
#some code
except (RuntimeError, TypeError, NameError):
#pass etc.
You can read the docs here Handling Exceptions
Exception handling can be at the place where the exception is thrown. So if check_output is throwing exception, you could handle below way,
def get_pid(name):
pid_ = None
try:
pid_ = check_output(["pidof",name])
except:
pass
return pid_
#logic to wait for pid_ to finish if found

Using pexpect to automate IP validations

My end goal is to log into a switch, get the CDP neighbor details written to a file. parse out the IP addresses and run pings on the IP address and output that to a file. I've searched the forums and found nothing really pertaining to switches. Mainly servers, and I've adapted from there. But Now I am stuck.
I get as far as getting logged into a switch and my login prompts seem to be written to a file but nothing else.
Please excuse errors in the code. Also, the second try block is a work in progress. I am new to python and learning as I go. I am open to any and all critiques and suggestions. I want to know what I have done incorrectly, fix it, and build from there.
Thanks in advance!
import pexpect
import getpass
import sys
import re
import os
import time
try:
switch = raw_input("Host: ")
un = raw_input("Username: ")
pw = getpass.getpass("Password: ")
options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'
prompt = "#"
connection_string = ("ssh %s#%s %s" % (un,switch,options))
child = pexpect.spawn(connection_string, timeout=5)
file_output = open('switch_output.txt','wb+')
child.logfile = file_output
rva12 = "";
child.expect(re.escape("password:"))
time.sleep(1)
child.sendline(pw)
time.sleep(1)
child.expect(prompt)
time.sleep(1)
child.sendline("no page")
time.sleep(1)
child.expect(prompt)
child.sendline("show cdp nei det")
time.sleep(1)
child.expect(prompt)
child.before
file_output.close()
except Exception as e:
print("Failed on login")
print(sys.exc_info()[0].__name__,
os.path.basename(sys.exc_info()[2].tb_frame.f_code.co_filename),
sys.exc_info()[2].tb_lineno)
print(e)
try:
f = open('switch_output.txt', 'r')
pattern_one = re.compile('.*A\d\n|\:\s\d{2,}\.\d{2,}\.\d{1,}.\d{1,}?')
for line in f:
matches = []
if pattern_one.search(line):
matches.append(line.split(':', 1)[-1].strip())
mp = re.compile('\d{2,}\.\d{2,}\.\d{1,}.\d{1,}')
with open('MDF1.txt', 'wb+') as file:
for match in matches:
if mp.search(match):
file.write(match+'\n')
file.close()
for line in open('MDF1.txt','r'):
child.expect(prompt)
child.sendline('ping '+ line)
time.sleep(10)
except Exception as e:
print(sys.exc_info()[0].__name__,
os.path.basename(sys.exc_info()[2].tb_frame.f_code.co_filename),
sys.exc_info()[2].tb_lineno)
print(e)

Hive UDF with Python - Print exception on shell

In the code below, Whenever exception is caught, I would like to exit from the program & print the exception on shell
#stream.py
import hashlib
import sys
import os
import importlib
for line in sys.stdin.readlines():
try:
inpFile = "temp.py"
execfile(inpFile)
line1 = line.strip('\n').split('\t')
print "\t".join(line1)
except:
#exception expected "temp.py:File Not Found", how do I exit the code & print the exception on console ?
sys.exit(1)
Here is the Transform query to call the UDF:
Create table newtable as Select TRANSFORM(id,name) USING
'python stream.py' as (id,name) from mytable;
ideas appreciated.
if you want to catch a specific type of exception (for examle an IOError) you can use
except IOError as e:
and access the error string with e.strerror
if you want to catch all exception you can use sys.exc_info()[0] to access the last error message
for example
try:
1/0
except:
print sys.exc_info()[0]
would print
<type 'exceptions.ZeroDivisionError'>
If you are expecting a specific exception then you should trap that, not everything, which is what you are doing. But there is nothing in your code to generate a "File Not Found" anyway!
EDIT: question code changed! We really do wish to trap "everything". I am using Exception, which traps "almost" everything, see https://docs.python.org/2/library/exceptions.html
This uses Python 2 syntax:
import sys
for line in sys.stdin.readlines():
try:
inpFile = "temp.py"
execfile(inpFile)
line1 = line.strip('\n').split('\t')
print "\t".join(line1)
except Exception as err:
print >> sys.stderr, err # print to stderr (python 2 syntax)
sys.exit(1)

Skip bad EOL in Python

I'm reading from CAT pipe in Linux, using subprocess:
stdout=subprocess.PIPE
so some line has BAD EOL, it's huge file and I want to skip such lines and go for the next one. how I can do this in Python?
PS: I always get:
SyntaxError: EOL while scanning string literal
and seems some socket stopped while writing to that file,because I see really huge spaces in the end of that file. Don't want to fix it, want to skip it
here is my code :
import sys,os
import subprocess
import traceback
import re
import ast
try :
cat = subprocess.Popen(["hadoop", "dfs", "-cat", "PATH TO FILE"], stdout=subprocess.PIPE)
for data in cat.stdout:
data = re.sub(' +',' ',data)
msg= ast.literal_eval(data)
if msg['some_string'] == 'some_string' :
print msg['status']
else :
continue
except :
print traceback.format_exc()
pass
exit()
so the output before the programs exits :
many empty spaces and ...
^
SyntaxError: EOL while scanning string literal
Here, try this:
import sys,os
import subprocess
import traceback
import re
import ast
try :
cat = subprocess.Popen(["hadoop", "dfs", "-cat", "PATH TO FILE"], stdout=subprocess.PIPE)
for data in cat.stdout:
data = re.sub(' +',' ',data)
try:
msg= ast.literal_eval(data)
if msg['some_string'] == 'some_string' :
print msg['status']
else :
continue
except SyntaxError:
continue #skip this line
except :
print traceback.format_exc()
pass
exit()
Hope it helps!
to skip errors you can just code smth like:
try:
your code
except {Your error}:
pass
or
try:
your code
except:
pass
for all errors
you could also use smth like:
import sys
import traceback
try:
{code}
except Exception:
_type, _value, _trace = sys.exc_info()
print "Type:\n\t{0}\nException:\n\t\t{1}\nTraceback:\n\t{2}".format(
_type, _value, traceback.format_tb(_trace))

How can I capture the return value of shutil.copy() in Python ( on DOS )?

I am trying to record the success or failure of a number of copy commands, into a log file. I'm using shutil.copy() - e.g.
str_list.append(getbitmapsfrom)
game.bigbitmap = "i doubt this is there.bmp"
str_list.append(game.bigbitmap)
source = '\\'.join(str_list)
shutil.copy(source, newbigbmpname)
I forced one of the copy commands in my script to fail, and it generated the error:
[Errno 2] No such file or directory: 'X:\PJ_public\PJ_Services\BSkyB-PlayJam\Content\P_NewPortal2009\1.0.0\pframes\i doubt this is is there.bmp'
This is great, but can I capture "Errno 2 No such file or directory" and write it to a log file? Does shutil.copy() return an integer value? - I don't see this described in the Python docs.
I guess also I want to be able to capture the return value, so that the script doesn't bomb out on a copy failure - I'm trying to make it continue regardless of errors.
Thanks.
You'll want to look at the exceptions section of the Python tutorial. In the case of shutil.copy() not finding one of the arguments, an IOError exception will be raised. You can get the message from the exception instance.
try:
shutil.copy(src, dest)
except IOError, e:
print "Unable to copy file. %s" % e
You will rarely see C-like return codes in Python, errors are signalled by exceptions instead.
The correct way of logging result is:
try:
shutil.copy(src, dest)
except EnvironmentError:
print "Error happened"
else:
print "OK"
try:
shutil.copy(archivo, dirs)
except EnvironmentError:
print "Error en el copiado"
escritura = "no se pudo copiar %s a %s \n" % (archivo, dirs)
else:
print "Copiado con exito"
escritura = "%s --> %s \n" % (archivo, dirs)
finally:
log = open("/tmp/errorcreararboldats.log", "a")
log.write(escritura)
log.close()

Categories

Resources