0

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)
Xonshiz
  • 1,307
  • 2
  • 20
  • 48
  • what is `nremoval` and `response` ? why do you write it instead of `lines` ? – furas Jan 25 '16 at 02:03
  • updated the code, had forgotten to add that line. `nremoval` is removing the `\n` character from the lines `fo` is reading from. I tried lines, it didn't work. It's not even showing any output for it. – Xonshiz Jan 25 '16 at 02:26
  • `readlines()` returns list and you convert this list into one string - print it to see what strange string you created. You can use `lines = lines.strip()` to get one line without `\n` and then you can send it to `pump.py` – furas Jan 25 '16 at 02:31
  • btw: you don't need `if __name__ =="__main__":` – furas Jan 25 '16 at 02:32
  • Okay, let me try making the changes. – Xonshiz Jan 25 '16 at 02:51
  • Do you have a compelling reason to call pump.py from standard in instead of just importing the relevant function and calling it directly in origin.py? – Paul Becotte Jan 25 '16 at 02:56
  • Because I'm not that advance right now. I'm beginning python and programming as a whole. So, this is the only only way I can think of. And the pump.py is something I didn't write. I'm trying to automate it, you can call it that – Xonshiz Jan 25 '16 at 02:57

1 Answers1

2

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)
furas
  • 134,197
  • 12
  • 106
  • 148
  • I tried his code, but, it's sending all the links at once. And hence, it's giving me an error : http://i.imgur.com/jw0dhBn.png – Xonshiz Jan 25 '16 at 02:51
  • Plus I had to remove 'python3' to make it work. I'm on python 2.7 and it's taking python3 as a file name. Maybe 'cause a lot of things changed in python 3 from python 2.7 – Xonshiz Jan 25 '16 at 02:52
  • try use `p.stdin.write(line)` – furas Jan 25 '16 at 02:55
  • This is getting weird now. When I remove 'python3', it s `[Error 193] %1 is not a valid Win32 application` and when I keep 'python3', it says `[Error 2] The system cannot find the file specified` – Xonshiz Jan 25 '16 at 03:02
  • However, If i use pump.bat, it doesn't show up any error. However, it's not passing any value as well. – Xonshiz Jan 25 '16 at 03:04
  • I have no Windows to test it. – furas Jan 25 '16 at 03:06
  • Yes, it works. Seems like I commented a line or something and hence it didn't work at that time. Thank You furas! – Xonshiz Jan 25 '16 at 03:11