This question already has answers here:
Is there any way to pass values to input prompt to the python script which was called by other python script?
(3 answers)
Closed 11 months ago.
Is there a way to auto select a user input option via task scheduler or within the script itself? I'm using a colleagues code and it would save some time if this could be achieved.
Example code below where one of these options needs to be selected.
while True:
choose_report = input(""" Please select the Report type
1) Weekly
2) Monthly
3) Yearly
4) Exit\n
Review type:> """)
I've tried argv but that changes the variables of the script, which is what I could do but I was wondering if there is a quicker way without editing the script variables.
You can do this in a couple of different ways:
Pipe user inputs from a file through stdin. For example: my_script.py < inputs.txt. This will work without needing to change the original script, but is a bit less clear and more fragile if you ever want to change the behavior of the script in the future.
Refactor the script to use argparse, and don't ask for interactive input when running from the task scheduler. Then you can specify all of the choices as script arguments, e.g. my_script.py --report-type=monthly. This will take more work up front, but makes the script very easy to change in the future.
Related
This question already has answers here:
Passing IPython variables as arguments to bash commands
(4 answers)
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
So I found this tool: https://github.com/elceef/dnstwist and I want to pass a list of domains into that tool then take the output and visualize it.
It operates through the command line, but how do I automate entering each domain and process the output automatically as well?
By the way I am using colab, so I would need a solution using jupyter notebooks!
Thanks!
If you want to do this programmatically I would not use the provided cli – although this would also be possible.
Instead you can create a small python script to do this.
Import dwintwist in your script and have a look at the main function in dwintwist.py to see how you call this.
Doesn't look to hard, but I don't know your programming skill level of course.
This question already exists:
Is there a way to run a script with 'unrecognizable' variablenames & funcitonnames? [closed]
Closed 2 years ago.
I wrote a project with many sub-sheets of python code. and need to run it on a p2p cloud-computing service because it needs performance. I dont want the 'host' to know what it is by trying to understand the variable names and function names.
Its about 1000s of variables and 100s of functions, so doing it via CTRL+r and then renaming gives a high risk of errors and takes a long time.
a) Is there a procedure in compiling to make the variable names (of a copy) unrecognizable? (e.g. ahjbeunicsnj instead of placeholder_1_for_xy_csv or kjbej() instead of save_csv())
or alternatively b) Is there a way to encrypt all the files and run it encrypted? (maybe .exe)
Yes it's possible. You can obfuscate the Python script. Programs like PyInstaller can create executables too. You didn't indicate that you researched that but it's an option.
Here's the official page on this topic which goes into far more detail: https://wiki.python.org/moin/Asking%20for%20Help/How%20do%20you%20protect%20Python%20source%20code%3F
Here's an answer on another StackExchange that's also relevant: https://reverseengineering.stackexchange.com/questions/22648/best-way-to-protect-source-code-of-exe-program-running-on-python
I'm a new Python user, and I'm sure this is a really basic question, but I can't find the answer anywhere. When people post Python code online it is often formatted like this:
In [1]: # some stuff
Out[1]:
# some more stuff
What are the In's, Out's, and the numbers? And why does my Python console not behave like that?
They are not Python code. They are IPython prompts, a popular Python add-on interactive shell.
Each line of code executed on the interactive prompt (denoted by In) is numbered, and so is the output produced (denoted by Out). You can then instruct IPython to refer back to those inputs and outputs for re-running code or re-using output.
See the Input caching system documentation>
IPython offers numbered prompts (In/Out) with input and output caching (also referred to as ‘input history’). All input is saved and can be retrieved as variables (besides the usual arrow key recall), in addition to the %rep magic command that brings a history entry up for editing on the next command line.
I'm looking to test a batch of python scripts made by different users. They're behaving in the same manor, asking for user input, it will be random but within a range I know ahead of time.
I want to write a script that executes each one of them, each of the scripts will give a prompt. Given the prompt, I want my script to respond accordingly. Is this even possible? How would I go about this?
I'd also like to detect errors, like the ones thrown by IDLE and log them to a file. Any ideas?
--Python 3 in use--
A similar questions was asked here, without a solution that fits my scenario Read output from a Python Executed Script
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling an external command in Python
I would like to call various programs from my Python script, like binary programs, but also other perl/python/ruby scripts, like wget, sqlmap and custom scripts.
The problem is that I would like the user to be able to change parameters of the underlying program. Let's take wget for example. Let's say I'm calling this program (note that all three parameters are dynamically inputted into the command):
wget www.google.com --user=user --password=pass
But I would also like the user to add custom parameters to the wget command. I guess the best way would be directly from a file, but I was wondering if something like this exists so that I won't reprogram everything by hand.
Also keep in mind that this is not just 1 program, but it could be up to 100 programs, maybe more. It needs to be extendable and not too complicated for the user to change.
Thanks
One quick example using subprocess.check_output
program = 'wget'
default_args = ['www.google.com']
user_args = []
subprocess.check_output(program + default_args + user_args)
Just be very carefull with this, do all the security checks before allowing any user to add parameters to a command.
You may also need shlex.split to split the user supplied arguments before adding them to subprocess call
If you want to have the defaults in external files you could do something like this:
with open('wget_defaults.txt') as i:
default_args = i.read().split(',')
Hope it helps