将参数从一个 python 脚本传递到另一个脚本

Passing argument from one python script to another

我总共有 3 个文件:clean.txt、origin.py 和 pump.py

Clean.txt 中有一些行(实际上是网站 links。例如:

www.link1.com
www.link2.com
www.link3.com
www.link4.com

origin.py 是逐行读取的脚本,我希望此脚本将 link(一次一个)发送到 pump.py

pump.py 是一个脚本,它要求我输入一个 link.

现在,我想这样做,以便我读取来自 clean.txt 的行(origin.py 正在执行此任务)并将它们一次发送到 pump.py。就像一个循环。 Origin.py 的代码是:

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)