Passing argument from one python script to another - python

I've got 3 files in total : clean.txt, origin.py and pump.py
Clean.txt has some lines in it (actually website links. For eg:
www.link1.com
www.link2.com
www.link3.com
www.link4.com
origin.py is the script that's reading lines one by one and I want this script to send the link (one at a time) to pump.py
pump.py is a script which asks me for one input, which is a link.
Now, I want to make it so that I read lines from clean.txt (origin.py is doing this task) and send them to the pump.py one at a time. Like a loop.
Code for Origin.py is :
fo = open("Clean.txt", "r")
nremoval = str(fo.readlines()).replace('\\n','')
for lines in fo :
if __name__ =="__main__":
response = nremoval
p = subprocess.Popen("pump.py", stdin = subprocess.PIPE)
time.sleep(2)
p.stdin.write(bytes(response, 'ascii'))
print("injected string :", response)

origin.py
import subprocess
import time
# open file
with open("Clean.txt", "r") as fo:
# read line by line
for line in fo:
# remove ENTER and SPACES from single line
line = line.strip()
# call "python pump.py"
p = subprocess.Popen(["python","pump.py"], stdin=subprocess.PIPE)
# or "python3 pump.py"
#p = subprocess.Popen(["python3","pump.py"], stdin=subprocess.PIPE)
#time.sleep(2)
# send line to standard input
#p.stdin.write(bytes(line, 'ascii')) # python 3
p.stdin.write(line) # python 2
print("injected string:", line)
pump.py
#line = input() # python 3
line = raw_input() # python 2
print("received:", line)

Related

How to split the csv file with line numbers which pass as parameter and save into different files

python script.py 2
Here each file will be having 2 lines. filename is having time, data,hour, second
I have 5 line in a csv file
I will take input from user as parameter
if user is giving 2 then my each file will be having 2 lines each.
first 2 line will be in first file, second two line in second file and last line is in third file
Last file will be having one line
Filename will be having hour minute second
below is the csv file
1,Network activity,ip-dst,80.179.42.44,,1,20160929
2,Payload delivery,md5,4ad2924ced722ab65ff978f83a40448e,,1,20160929
3,Network activity,domain,alkamaihd.net,,1,20160929
4,Payload delivery,md5,197c018922237828683783654d3c632a,,1,20160929
5,Network activity,domain,dnsrecordsolver.tk,,1,20160929
I need to divide in to 3 different files, split number of lines has to taken as argument
Below is the approach
take the division line as parameter and save with data with time stamp
Below is the code for saving the data with date.hour,time. I need some logic to divide the line and continue from there
import csv
import time
import sys
sourceData = "Oil.csv"
def GetLineCount():
with open(sourceData) as f:
for i, l in enumerate(f):
pass
return i
def MakeLog(startLine, numLines):
destData = time.strftime("%Y%m%d-%H%M%S.log")
with open(sourceData, 'r') as csvfile:
with open(destData, 'w') as dstfile:
reader = csv.reader(csvfile)
writer = csv.writer(dstfile)
next (reader) #skip header
python script.py 2
then 3 files will be generate with 2 lines in each file and last file will be having one
Expected out
3 text file will be generate 2 lines each
There is a linux split command
split -l 2 Oil.csv
would split into files of 2 lines each.
For appending hour, minute, and second to filenames,
split -l 2 Oil.csv --additional-suffix=`date +"%H:%M:%S"`
And here's how you would do it in Python3.
import argparse
import time
from itertools import zip_longest
def grouper(n, iterable, fill_value=None):
args = [iter(iterable)] * n
return zip_longest(fillvalue=fill_value, *args)
def splitter(n_lines, file):
with open(file) as f:
for i, payload in enumerate(grouper(n_lines, f, fill_value=''), 1):
f_name = f"{time.strftime('%Y%m%d-%H%M%S')}_{i*n_lines}.log"
with open(f_name, 'w') as out:
out.writelines(payload)
def get_parser():
parser = argparse.ArgumentParser(description="File splitter")
parser.add_argument("file", metavar="FILE", type=str, help="Target file to be chopped up")
parser.add_argument("n_lines", type=int, default=2, help="Number of lines to output per file")
return parser
def command_line_runner():
parser = get_parser()
args = vars(parser.parse_args())
splitter(args['n_lines'], args['file'])
if __name__ == "__main__":
command_line_runner()
Sample run: python3 main.py sample.csv 2 produces 3 files:
20200921-095943_2.log
20200921-095943_4.log
20200921-095943_6.log
The first two have two lines each and the last one, well, one line.
The contents of sample.csv is as in your example:
1,Network activity,ip-dst,80.179.42.44,,1,20160929
2,Payload delivery,md5,4ad2924ced722ab65ff978f83a40448e,,1,20160929
3,Network activity,domain,alkamaihd.net,,1,20160929
4,Payload delivery,md5,197c018922237828683783654d3c632a,,1,20160929
5,Network activity,domain,dnsrecordsolver.tk,,1,20160929
You can find the line numbers to split at n lines with (linenumber+1) % n:
import sys
import datetime
def save_file(newcontent):
savenewfile = str(datetime.datetime.now()) + ".log"
with open(savenewfile, 'w') as dstfile:
dstfile.write(newcontent)
split_at = int(sys.argv[1])
newcontent = ""
with open("Oil.csv", "r") as f:
for i, line in enumerate(f):
newcontent += line
if (i+1) % split_at == 0: # i+1 because i starts at 0
save_file(newcontent)
newcontent = ""
# save remaining lines
if newcontent:
save_file(newcontent)
save_file(newcontent) will be your function to save a string to a new file.
Your timestring %Y%m%d-%H%M%S won't make a unique file name. You can add a counter or use str(datetime.datetime.now()) + ".log" instead.

How to prompt user that asks a user for a file name?

I am going through Intro to Programming so basic stuff here, I have an assignment to "write a program that asks a user for a file name and then displays the first 5 lines of the file," I just can't figure out how to use the input command in this situation and then transfer to open()
Edit: Sorry here is a code snippet I had, I just don't get how to apply input from here.
def main():
#This function writes to the testFile.docx file
outfile = open('testFile.docx', 'w')
outfile.write('Hello World\n')
outfile.write('It is raining outside\n')
outfile.write('Ashley is sick\n')
outfile.write('My dogs name is Bailey\n')
outfile.write('My cats name is Remi\n')
outfile.write('Spam Eggs and Spam\n')
outfile.close()
infile = open('testFile.docx', 'r')
testFileContent = infile.read()
infile.close()
print(testFileContent)
main()
First, we ask for a filename. Then we use the try clause, which checks whether the file exists. If it does it will print 5 lines. If it does not, it will print No such a file found!
x = input('Enter a file name')
try:
with open(x) as f:
data = f.readlines()
for i in range(5):
print(data[i])
except:
print('No such a file found!')
Using a simple function,
def hello_user():
user_input = input('Enter file name: ')
try:
with open(user_input, 'r') as f:
data = f.readlines()
data = data[:5]
for o in data:
print(o.strip())
except FileNotFoundError:
print('Not found ')
hello_user()
It asks for a file name
If the file exists in the same directory the script is running, it opens the file and read each lines (white lines inclusive)
We select only the first 5 lines
We iterate through the list and remove the extra whitespace character(e.g \n).
If the file was not found, we catch the exception.
input() is used to receive input from the user. Once we recieve the input, we use the open() method to read the file in read mode.
def main():
file = input("Please enter a file name")
with open(file, 'r') as f:
lines = f.readlines()
print(lines[:5])
The with statement makes sure that it closes the file automatically without explicitly calling f.close()
The method f.readlines() returns an array containing the lines in the file.
The print() statement prints the first 5 lines of the file.

I have list of adresses which i need to nslookup and send to csv

I am trying to do nslookup for addresses in my adrese.txt file and I would like to save them as .csv. Currently my biggest problem is that it only does nslookup for only one address and not all. It just exits with 0 and in my file there is only one adress. I am new to python and got no idea how to fix it. Also replacing .txt with csv in output file would be nice too.
edit: adress getting from text file works, second part is the problem, don't know why
import subprocess
f = open("adrese.txt")
next = f.read()
ip=[]
while next != "":
ip.append(next)
next = f.read()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
The reason why its doing this is because while next != "" is not doing what you want it to.
Instead, consider this:
import subprocess
with open('adrese.txt') as i, open('nslookup.txt', 'w') as o:
for line in i:
if line.strip(): # skips empty lines
proc = subprocess.Popen(["nslookup", line.strip()],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o.write('{}\n'.format(proc.communicate()[0]))
print('Done')
Your are actually not looping through all entries in your adrese.txt
ip = []
f = open("adrese.txt")
for line in f:
ip.append(line)
f.close()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
You can use check_call and redirect the stdout directly to a file:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
subprocess.check_call(["nslookup", line],
stdout=out)
You never use stderr so there is no point capturing it, if there are any non zero exit status you can catch a CalledProcessError:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
try:
subprocess.check_call(["nslookup", line],
stdout=out)
except subprocess.CalledProcessError:
pass

how to read line by line from a textfile and pass the value using python

I have a file "BatchLink.txt" which contains urls in new lines.I want to read these lines by lines and pass the argument linewise to a batch script.
Let's say, my batchlink.txt contains this data :-
http://link1
http://link2
http://link3
http://link4
Now, I want to read linewise and send this to a batch file one line at a time.This question is the continuation of my previous question Here.
Right Now, I have this code :-
import requests
from bs4 import BeautifulSoup
import subprocess
file = open("BatchLinks.txt", "w")
r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})
for episode in subtitles:
x = episode.find_all('a')
for a in x:
#print a['href']
z = a['href']
l = 'http://www.crunchyroll.com'+ z
print l
file.write(l+'\n')
print '-----------------------------------------------------------------------------'
file.close()
file = open('BatchLinks.txt', "r")
lines = file.readlines()
print lines
if __name__ =="__main__":
response = lines
print(lines)
p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
time.sleep(1)
p.stdin.write(response) #Answer the question
time.sleep(20)
But, right now, the problem is that it reads the lines simultaneosuly and send it to the batch file.Which returns me the output [] . I can't seem to get it work.Any help/guidance would be appreciated.
file = open('BatchLinks.txt', "r")
lines = file.readlines()
Change that to a more up-to-date version:
with open('BatchLinks.txt', "r") as inf:
for line in inf:
do something with line
file = open('BatchLinks.txt', "r")
lines = file.readlines()
Change that to a more up-to-date version:
with open('BatchLinks.txt', "r") as inf:
for line in inf:
do something with line
This is very basic stuff. Use the manual! https://docs.python.org/2/library/stdtypes.html#file-objects
I see two issues: you need to write to the pipe line by line, and need to close the pipe after done.
Replace the following line:
p.stdin.write(response) #Answer the question
with this one:
for line in response:
p.stdin.write(line)
p.stdin.close()
When you do - lines = file.readlines() - it reads all the lines from the file and returns it as a list , so lines is a list of all the lines in the file.
Then you are doing - p.stdin.write(response) - this sends the complete list over to the other process, you should iterate over the lines and send each line to a new process.
Example -
if __name__ =="__main__":
for line in lines:
print(line)
p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
time.sleep(1)
p.stdin.write(line.strip())
time.sleep(20)

MD5 decrypt script

__author__ = 'Zane'
import hashlib
import sys
if (len(sys.argv)!=2 ) or (len(sys.argv[1])!= 32):
print("[---] md5cracker.py & hash")
sys.exit(1)
crackedmd5 = sys.argv[1]
# open a file and read its contents
f = open('file.txt')
lines = f.readline()
f.close()
for line in lines:
cleanline = line.rstrip()
hashobject = hashlib.md5(cleanline)
if (hashobject==crackedmd5):
print('Plain text password for ' + crackedmd5 + "is " + hashobject + '\n')
I get no error with exit code 1 and i do not know where i get it wrong
Your program exits with status code one because you told it so (roughly on line 8):
sys.exit(1)
Pythons code structure is based on indent of lines. For now your whole code is part of the if (len(sys.argv)!=2 ) or (len(sys.argv[1])!= 32): condition.
You need to unindent all lines with one tab starting from crackedmd5 = sys.argv[1]
EDIT
You also used lines = f.readline() which will read only one line and so for line in lines will iterate over every single char in that line and not over multiple lines. You need to use lines = f.readlines() instead.

Categories