I'm creating my first python tool, if you want to call it that. (I work in IT Security, btw)
It was going well until I tried to call a function from the main function. I'm sure it's something simple that I'm missing :(. Could someone point me in the right direction? Then feel free to point and laugh.
#!/usr/bin/python
import sys, getopt, socket
def usage():
print "-h --help: help\n"
print "-f --file: File to read bruteforce domain list from.\n"
print "-p --proxy: Proxy address and port. e.g http://192.168.1.64:8080\n"
print "-d --domain: Domain to bruteforce.\n"
print "-e: Turn debug on.\n"
sys.exit()
def main(argv):
file = None
proxy = None
domain = None
try:
opts, argv =getopt.getopt(argv, "h:f:p:d:e",["help", "file=", "proxy=", "domain="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-f", "--file"):
file = arg
elif opt in ("-p", "--proxy"):
proxy = arg
elif opt in ("-d", "--domain"):
domain = arg
elif opt in '-e':
global _debug
_debug = 1
else:
assert Flase, "Unhandled option"
print fread.flist
def fread(file, *args):
flist = open(file).readlines()
return
if __name__ == "__main__":
main(sys.argv[1:])
The issue is with your statement print fread.flist. You can't access variables assigned inside functions this way. Instead, change your fread() function to the following:
def fread(file, *args):
flist = open(file).readlines()
return flist
and change the statement above to
print fread(file)
where file is the file you want to access.
Related
I am trying to rerun my python script every 10 seconds. I have tried various things with the time.sleep function but nothing seems to be working. I have tried different variants of time.sleep:
def job()
# script
if __name__ == '__main__':
while True:
job()
time.sleep(10)
but not finding the correct place/way to implement it. I don't want to rerun a function, but the full code.
This is the script I am trying to rerun:
import...
def main():
# parse the command line arguments
args = ConfigArgumentParser(description=__doc__).parse_args()
if _debug: _log.debug("initialization")
if _debug: _log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(
objectName=args.ini.objectname,
objectIdentifier=('device', int(args.ini.objectidentifier)),
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
segmentationSupported=args.ini.segmentationsupported,
vendorIdentifier=int(args.ini.vendoridentifier),
)
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make some random input objects
for i in range(1, RANDOM_OBJECT_COUNT + 1):
ravo = RandomAnalogValueObject(
objectIdentifier=('analogValue', i),
objectName='Temp%d' % (i,),
)
this_application.add_object(ravo)
# make sure they are all there
_log.debug(" - object list: %r", this_device.objectList)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()
If you need the whole thing to run (even bootstrap code), you can use a shell script to do it.
This won't refresh environment variables though.
#!/usr/bin/env bash
while true; do
sleep 10
python script.py
done
If you need to refresh environment variables (you seem to need it because of RANDOM_OBJECTS), add eval "$(exec /usr/bin/env -i "${SHELL}" -l -c "export")" to the mix. Source: https://unix.stackexchange.com/a/581684
Try this:
if __name__ == "__main__":
time.sleep(10)
main()
What you can do is just restart your whole script after 10s.
Applied to your code it would look like this:
import os
import time
import json
import sys
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run
from bacpypes.primitivedata import Real
from bacpypes.object import AnalogValueObject, Property, register_object_type
from bacpypes.errors import ExecutionError
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
# Read Json
with open('ObjectList.json') as json_file:
data = json.load(json_file)
def get_present_value(no):
for a in data['AnalogValues']:
if a['ObjectIdentifier'] == int(no):
return a['PresentValue']
return None
ai_1 = (get_present_value(1))
print(ai_1)
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# settings
RANDOM_OBJECT_COUNT = int(os.getenv('RANDOM_OBJECT_COUNT', 1))
#
# RandomValueProperty
#
class RandomValueProperty(Property):
def __init__(self, identifier):
if _debug: RandomValueProperty._debug("__init__ %r", identifier)
Property.__init__(self, identifier, Real, default=0.0, optional=True, mutable=False)
def ReadProperty(self, obj, arrayIndex=None):
if _debug: RandomValueProperty._debug("ReadProperty %r arrayIndex=%r", obj, arrayIndex)
# access an array
if arrayIndex is not None:
raise ExecutionError(errorClass='property', errorCode='propertyIsNotAnArray')
# return a random value
value = ai_1
if _debug: RandomValueProperty._debug(" - value: %r", value)
return value
def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False):
if _debug: RandomValueProperty._debug("WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", obj, value,
arrayIndex, priority, direct)
raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')
bacpypes_debugging(RandomValueProperty)
#
# Random Value Object Type
#
class RandomAnalogValueObject(AnalogValueObject):
properties = [
RandomValueProperty('presentValue'),
]
def __init__(self, **kwargs):
if _debug: RandomAnalogValueObject._debug("__init__ %r", kwargs)
AnalogValueObject.__init__(self, **kwargs)
bacpypes_debugging(RandomAnalogValueObject)
register_object_type(RandomAnalogValueObject)
#
# __main__
#
def main():
# parse the command line arguments
args = ConfigArgumentParser(description=__doc__).parse_args()
if _debug: _log.debug("initialization")
if _debug: _log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(
objectName=args.ini.objectname,
objectIdentifier=('device', int(args.ini.objectidentifier)),
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
segmentationSupported=args.ini.segmentationsupported,
vendorIdentifier=int(args.ini.vendoridentifier),
)
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make some random input objects
for i in range(1, RANDOM_OBJECT_COUNT + 1):
ravo = RandomAnalogValueObject(
objectIdentifier=('analogValue', i),
objectName='Temp%d' % (i,),
)
this_application.add_object(ravo)
# make sure they are all there
_log.debug(" - object list: %r", this_device.objectList)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()
time.sleep(10)
os.execl(sys.executable, sys.executable, * sys.argv) # Your script restarts after 10s with this
I was trying to make tetris game using reinforcement learning. I get this name undefined error.Thanks in advance for the solution
class TetrisApp
...........
..........
other code
..........
..........
def main(argv):
cp = ''
try:
opts, args = getopt.getopt(argv,"hc:c:",["computer_player="])
except getopt.GetoptError:
print 'tetris.py -c[--computer_player] <True>|<False>'
sys.exit(2)
if len(opts) == 0 :
play(True)
for opt, arg in opts:
if opt == '-h':
print 'tetris.py -c <True>|<False>'
sys.exit()
elif opt in ("-c", "--computer_player"):
cp = arg
if cp == 'True':
play(True)
else:
play(False)
if __name__ == "__main__":
main(sys.argv[1:])
for calling Class methods Outside the class Use Class Object.
t=TetrisApp()
t.main(sys.argv[1:])
and use self in method def main(self,argv):
So I'm working on an assignment in a python class that I'm taking, but have gotten stuck with something I can't really find any further information about (neither on SO, Google or in the courseware).
I need help with how to handle arguments with multiple types of syntaxes - like [arg] and < arg >, which is something I've been unable to find any further information about.
Here is an example use-case that SHOULD work.
>>> ./marvin-cli.py --output=<filename.txt> ping <http://google.com>
>>> Syntax error near unexpected token 'newline'
The below code works fine for any use-case where I haven't defined any further output than writing to the console:
# Switch through all options
try:
opts, args = getopt.getopt(sys.argv[1:], "hsv", ["help","version","silent", "get=", "ping=", "verbose", "input=", "json"])
for opt, arg in opts:
if opt in ("-h", "--help"):
printUsage(EXIT_SUCCESS)
elif opt in ("-s", "--silent"):
VERBOSE = False
elif opt in ("--verbose"):
VERBOSE = True
elif opt in ("--ping"):
ping(arg)
elif opt in ("--input"):
print("Printing to: ", arg)
else:
assert False, "Unhandled option"
except Exception as err:
print("Error " ,err)
print(MSG_USAGE)
# Prints the callstack, good for debugging, comment out for production
#traceback.print_exception(Exception, err, None)
sys.exit(EXIT_USAGE)
#print(sys.argv[1])
Example usage:
>>> ./marvin-cli.py ping http://google.com
>>> Latency 100ms
And this is a snippet showing how the ping works:
def ping(URL):
#Getting necessary imports
import requests
import time
#Setting up variables
start = time.time()
req = requests.head(URL)
end = time.time()
#printing result
if VERBOSE == False:
print("I'm pinging: ", URL)
print("Received HTTP response (status code): ", req.status_code)
print("Latency: {}ms".format(round((end - start) * 1000, 2)))
[] and <> are commonly used to visually indicate option requirement. Typically [xxxx] means the option or argument is optional and <xxxx> required.
The sample code you provide handles option flags, but not the required arguments. Code below should get you started in the right direction.
try:
opts, args = getopt.getopt(sys.argv[1:], "hsv", ["help", "version", "silent", "verbose", "output=", "json"])
for opt, arg in opts:
if opt in ("-h", "--help"):
printUsage(EXIT_SUCCESS)
elif opt in ("-s", "--silent"):
VERBOSE = False
elif opt in ("--verbose"):
VERBOSE = True
elif opt in ("--output"):
OUTPUTTO = arg
print("Printing to: ", arg)
else:
assert False, "Unhandled option"
assert len(args) > 0, "Invalid command usage"
# is there a "<command>" function defined?
assert args[0] in globals(), "Invalid command {}".format(args[0])
# pop first argument as the function to call
command = args.pop(0)
# pass args list to function
globals()[command](args)
def ping(args):
#Getting necessary imports
import requests
import time
# validate arguments
assert len(args) != 1, "Invalid argument to ping"
URL = args[0]
#Setting up variables
start = time.time()
req = requests.head(URL)
end = time.time()
#printing result
if VERBOSE == False:
print("I'm pinging: ", URL)
print("Received HTTP response (status code): ", req.status_code)
print("Latency: {}ms".format(round((end - start) * 1000, 2)))
This is the program I have:
from sys import argv
script, arg1 = argv
def program(usr_input, arg1):
if(usr_input == arg1):
print "CLI argument and user input are identical"
else:
print "CLI argument and user input aren't identical"
if arg1 != "":
usr_input = raw_input("enter something: ")
program(usr_input, arg1)
else:
print "You have not entered a CLI argument at all."
I get:
Traceback (most recent call last):
File "filename.py", line 3, in <module>
script, arg1 = argv
ValueError: need more than 1 value to unpack
How can I detect the lack of command line argument and throw an error/exception instead of receiving this error?
I would recommend just checking the program args in the __main__ location of your script, as an entry point to the entire application.
import sys
import os
def program(*args):
# do whatever
pass
if __name__ == "__main__":
try:
arg1 = sys.argv[1]
except IndexError:
print "Usage: " + os.path.basename(__file__) + " <arg1>"
sys.exit(1)
# start the program
program(arg1)
You can handle the exception:
In [6]: def program(argv):
try:
script, argv1 = argv
except ValueError:
print("value error handled")
...:
In [7]: program(argv)
value error handled
try this:
script = argv[0]
try:
arg1 = argv[1]
except:
arg1 = ''
You could use a try statement there:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
class MyError(Exception):
def __init__(self, value):
self.error_string = value
def __str__(self):
return eval(repr(self.error_string))
try:
script, arg1 = sys.argv
except ValueError:
raise MyError, "Not enough arguments"
Seeing that sys.argv is a list you should check the length of the list to make sure it is what you wish it to be. Your script with minor changes to check the length:
from sys import argv
def program(usr_input, arg1):
if(usr_input == arg1):
print "CLI argument and user input are identical"
else:
print "CLI argument and user input aren't identical"
if len(argv)== 2:
arg1 = argv[1]
usr_input = raw_input("enter something: ")
program(usr_input, arg1)
else:
print "You have not entered a CLI argument at all."
I'm writing a Python script that may or may not (depending on a bunch of things) run for a long time, and I'd like to make sure that multiple instances (started via cron) don't step on each others toes. The logical way to do this seems to be a PID-based lockfile… But I don't want to re-invent the wheel if there is already code to do this.
So, is there a Python module out there which will manage the details of a PID-based lockfile?
This might be of help to you: lockfile
If you can use GPLv2, Mercurial has a module for that:
http://bitbucket.org/mirror/mercurial/src/tip/mercurial/lock.py
Example usage:
from mercurial import error, lock
try:
l = lock.lock("/path/to/lock", timeout=600) # wait at most 10 minutes
# do something
except error.LockHeld:
# couldn't take the lock
else:
l.release()
i've been pretty unhappy with all of those, so i wrote this:
class Pidfile():
def __init__(self, path, log=sys.stdout.write, warn=sys.stderr.write):
self.pidfile = path
self.log = log
self.warn = warn
def __enter__(self):
try:
self.pidfd = os.open(self.pidfile, os.O_CREAT|os.O_WRONLY|os.O_EXCL)
self.log('locked pidfile %s' % self.pidfile)
except OSError as e:
if e.errno == errno.EEXIST:
pid = self._check()
if pid:
self.pidfd = None
raise ProcessRunningException('process already running in %s as pid %s' % (self.pidfile, pid));
else:
os.remove(self.pidfile)
self.warn('removed staled lockfile %s' % (self.pidfile))
self.pidfd = os.open(self.pidfile, os.O_CREAT|os.O_WRONLY|os.O_EXCL)
else:
raise
os.write(self.pidfd, str(os.getpid()))
os.close(self.pidfd)
return self
def __exit__(self, t, e, tb):
# return false to raise, true to pass
if t is None:
# normal condition, no exception
self._remove()
return True
elif t is PidfileProcessRunningException:
# do not remove the other process lockfile
return False
else:
# other exception
if self.pidfd:
# this was our lockfile, removing
self._remove()
return False
def _remove(self):
self.log('removed pidfile %s' % self.pidfile)
os.remove(self.pidfile)
def _check(self):
"""check if a process is still running
the process id is expected to be in pidfile, which should exist.
if it is still running, returns the pid, if not, return False."""
with open(self.pidfile, 'r') as f:
try:
pidstr = f.read()
pid = int(pidstr)
except ValueError:
# not an integer
self.log("not an integer: %s" % pidstr)
return False
try:
os.kill(pid, 0)
except OSError:
self.log("can't deliver signal to %s" % pid)
return False
else:
return pid
class ProcessRunningException(BaseException):
pass
to be used something like this:
try:
with Pidfile(args.pidfile):
process(args)
except ProcessRunningException:
print "the pid file is in use, oops."
I know this is an old thread, but I also created a simple lock which only relies on python native libraries:
import fcntl
import errno
class FileLock:
def __init__(self, filename=None):
self.filename = os.path.expanduser('~') + '/LOCK_FILE' if filename is None else filename
self.lock_file = open(self.filename, 'w+')
def unlock(self):
fcntl.flock(self.lock_file, fcntl.LOCK_UN)
def lock(self, maximum_wait=300):
waited = 0
while True:
try:
fcntl.flock(self.lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
return True
except IOError as e:
if e.errno != errno.EAGAIN:
raise e
else:
time.sleep(1)
waited += 1
if waited >= maximum_wait:
return False
I believe you will find the necessary information here. The page in question refers to a package for building daemons in python: this process involves creating a PID lockfile.
There is a recipe on ActiveState on creating lockfiles.
To generate the filename you can use os.getpid() to get the PID.
You can try PID: https://pypi.org/project/pid/
As the documentation shows, you can lock a function simply adding the decorator #pidfile() on the top of function/method name.
from pid.decorator import pidfile
#pidfile()
def main():
pass
if __name__ == "__main__":
main()
The default location for pidfile self check (the file who says if you can execute the code or not) is '/var/run'. You can change it as follows:
#pidfile(piddir='/path/to/a/custom/location')
For other params, see: https://github.com/trbs/pid/blob/95499b30e8ec4a473c0e6b407c03ce644f61c643/pid/base.py#L41
Unfortunatly, this lib's documentation is a little bit poor.