How do I use a text file as input in Python? - python

This may seem like a simple question but this is the first time I have touched Python so bear with me.
I created a simple bash script to do some SMTP enumeration and have been trying to convert it into a Python script. The bash script was:
And so far the Python script I have is this:
But right now, I have to type in each username individually once and the script closes. I have created a simple text file with a bunch of possible usernames and want to be able to use all the usernames in that file instead of typing them in individually one by one but am not sure on how to do that.

with open('users.txt') as users:
for user in users:
s.send(...)

Related

Sharing Python script that reads a .txt file on my computer

I have created a python script that works really well. It opens a .txt file and picks a word a random before using that for a quiz. If I want to share this code do I need to share the .txt file also and does the receiver have to change the path to the .txt file in the code to the location of the txt file on their computer?
Other than putting the words as a list directly in the code is there a way of referencing the .txt file to the code in such a way that it will work on any computer?
You can change the hardcoded path and replacing it using a variable that will reference the file path to your text file and ask for the input when the scripts, in various ways,
use input() to ask for user input
read as an CLI argument that user triggers the script
maintain a user configuration file and have the script read it
...
whatever ways you can think of that is efficient and suitable for your use case

How to modify all output files to conceal passwords in Robot Framework?

I'd like to conceal passwords from output files in Robot Framework.
In particular, I'm looking for a native possibility (not multiple commands):
to run a robot framework test retrieving one or more passwords from a vault through a custom keyword
and to remove in the output files (output.xml, log.html and report.html) all the strings equal to the password(s) retrieved.
I managed to do it for output.xml through --prerebotmodifier and a simple Python script I made, but the html files (log and report) are generated after the call to the Python script and so passwords are not concealed in there.
It's not possible to use --removekeywords since the password could be used somewhere else in the test and with DEBUG or TRACE it would be shown in the logs.
Another solution would be to run the Python script in a separate command (e.g. through ||) but this is not what I'd like to achieve.
robot --prerebotmodifier lib/password_clean.py -L TRACE testConceal.robot
Test to get password
${password}= get password ${SOME_PARAMETERS}
Log To Console ${password}
The expected result would be not to see the value of ${password} in output.xml, log.html and report.html with one Robot Framework native command.
You could use --removekeywords (https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#removing-keywords)
e.g.
--removekeywords get password
You could use NAME:
--removekeywords NAME:*password*. Any keywords with 'password' in title get removed.
I found a quick win which would be to use --listener instead of --prerebotmodifier. Still working on it, though.

How to set user defined permisiions when creating an excel file(.xlsx) using python or in linux

I wanted to create an excel document with some user defined permission(Read, write, execute) when it is being created using python or through linux/windows terminal. Or is there any way to extract the properties of excel file which contains permissions and change the permission?
Is there any way of creating it?
please find the attached image for reference.
Any sort of idea would be really helpful.

Data storage for standalone python application

I want to make a python program (with a PyQt GUI, but I don't know whether that is relevant) that has to save some information that I want to store even when the program closes. Example for information I want to store:
The user can search for a file in a file dialog window. I want to start the file dialog window in the previously used directory, even if the program is closed in between file searches.
The user can enter their own categories to sort items, building up on some of my predefined categories. These new categories should be available the next time the program starts.
Now I'm wondering what the proper way to store such information is. Should I use pickle? A proper database (I know a tiny bit of sqlite3, but would have to read up on that)? A simple text file that I parse myself? One thing for data like in example 1., another for data like in example 2.?
Also, whatever way to store it I use, where would I put that file?
I'm asking in the context that I might want to later make my program available to others as a standalone application (using py2app, py2exe or PyInstaller).
Right now I'm just saving a pickle file in the directory that my .py file is in, like this answer reconmends, but the answer also specifically mentions:
for a personal project it might be enough.
(emphasis mine)
Is using pickle also the "proper, professional" way, if I want to make the program available to other people as a standalone application?
Choice depends on your approach to data you store, which is yours?:
user should be able to alter it without usage of my program
user should be prevented from altering it with program other than my program
If first you might consider deploying JSON open-standard file format, for which Python has ready library called json. In effect you get text (which you can save to file) which is human-readable and can be edited in text editor. Also there exist JSON file viewers and editors which made viewing/editing of JSON files easier.
I think SQLite3 is the better solution in this case as Moldovan commented.
There is a problem in pickle, sometimes pickling format can be change across python versions and there are greater advantages of using sqlite3.

Call python script from Jira while creating an issue

Let say I'm creating an issue in Jira and write the summary and the description. Is it possible to call a python script after these are written that sets the value for another field, depending on the values of the summary and the description?
I know how to create an issue and change fields from a python script using the jira-python module. But I have not find a solution for using a python script while editing/creating the issue manually in Jira. Does anyone have an idea of how I manage that?
I solved the problem by using the Add-on Script runner in Jira. There I created a scripted field in Groovy and called the command prompt and run the python script from there. Here is a simple example of the script
def process = ['cmd', '/c', 'filepathToPython.exe', 'filepathToPythonFile.py'].execute()
process.waitfor()
return p.text
process?.err?.text can be used instead of process.text if one want to see eventual error messages.
Take a look at JIRA webhooks calling a small python based web server?

Categories

Resources