在Python中将进程作为线程与网络一起使用
基本上,我的想法是编写某种基本服务器,在那里我可以连接到我的计算机,然后远程运行命令。这似乎不是什么大问题;但后来我有了一个聪明的想法,下一步逻辑上将是添加某种线程,以便生成多个连接 我读到,由于GIL,在Python中将进程作为线程与网络一起使用,python,multithreading,networking,process,Python,Multithreading,Networking,Process,基本上,我的想法是编写某种基本服务器,在那里我可以连接到我的计算机,然后远程运行命令。这似乎不是什么大问题;但后来我有了一个聪明的想法,下一步逻辑上将是添加某种线程,以便生成多个连接 我读到,由于GIL,多处理。Process将是最好的尝试。我不完全理解线程,很难找到好的文档;所以我只是在扔东西,想弄清楚它是怎么工作的 好吧,看来我可能快要做对了;但我有一种感觉,我几乎不可能正确地做到这一点。我的程序现在确实允许多个连接,这在我第一次开始使用线程时是不允许的;但是,一旦建立了一个连接,然后又建立
多处理。Process
将是最好的尝试。我不完全理解线程,很难找到好的文档;所以我只是在扔东西,想弄清楚它是怎么工作的
好吧,看来我可能快要做对了;但我有一种感觉,我几乎不可能正确地做到这一点。我的程序现在确实允许多个连接,这在我第一次开始使用线程时是不允许的;但是,一旦建立了一个连接,然后又建立了另一个连接,第一个连接就不再能够向服务器发送命令。如果有人能给我任何帮助,或者给我指出我需要学习和理解的正确方向,我将不胜感激
这是我的密码:
class server:
def __init__(self):
self.s = socket.socket()
try:
self.s.bind(("",69696))
self.s.listen(1)
except socket.error,(value,message):
if self.s:
self.s.close()
def connection(self):
while True:
client , address = self.s.accept()
data = client.recv(5)
password = 'hello'
while 1:
if data == password:
subprocess.call('firefox')
client.close()
else:
client.send('wrong password')
data = client.recv(5)
p = Process(target=x.connection())
p.start()
x = server()
if __name__ == '__main':
main()
这个答案只适用于unix或类似unix的操作系统(windows没有我们使用的
os.fork()
)
在unix平台上执行这些操作的最常见方法之一是,在主进程继续侦听请求的同时,使用一个新进程来处理客户端连接
下面是一个可以同时处理多个连接的简单echo服务器的代码。您只需修改handle\u client\u connection()
即可满足您的需要
import socket
import os
class ForkingServer:
def serve_forever(self):
self.s = socket.socket()
try:
self.s.bind(("", 9000))
self.s.listen(1)
except socket.error, (value,message):
print "error:", message
if self.s:
self.s.close()
return
while True:
client,address = self.s.accept()
pid = os.fork()
# You should read the documentation for how fork() works if you don't
# know it already
# The short version is that at this point in the code, there are 2 processes
# completely identical to each other which are simulatenously executing
# The only difference is that the parent process gets the pid of the child
# returned from fork() and the child process gets a value of 0 returned
if pid == 0:
# only the newly spawned process will execute this
self.handle_client_connection(client, address)
break
# In the meantime the parent process will continue on to here
# thus it will go back to the beginning of the loop and accept a new connection
def handle_client_connection(self, client,address):
#simple echo server
print "Got a connection from:", address
while True:
data = client.recv(5)
if not data:
# client closed the connection
break
client.send(data)
print "Connection from", address, "closed"
server = ForkingServer()
server.serve_forever()
这是在哪个平台上运行的?unix/linux?或者windows?PS:如果条件
data==password
为真,则而1:
循环将无限继续。它将无限期地不断产生firefox进程。你可能想在client.close()
之后添加一个break
,主要是我在Linux/Ubuntu上尝试过,但我也在我的VirtualBox Windows 7上尝试过。是的,我知道,我添加它的原因是为了确保我能够检查我是否因为正确的原因而断开连接。或者,我想要断开连接的原因,如果这有道理的话。我没有考虑添加中断,但会这样做。考虑避免解决一些与编写网络应用相关的乏味问题。水变得越来越深!首先是线程,然后是进程更好,现在是os.fork()。这么多选择,我该怎么决定学什么!不过,感谢您的回复,这很好用,而且看起来比线程化要简单得多。您可以通过更方便的方式(因为windows上不存在os.fork()
)使用Process()
)实现与os.fork()
相同的效果。不过,我最熟悉的是os.fork()
,现在是凌晨2点,所以我不想继续就我没有经验的事情提供建议:)我完全理解,了解os.fork()似乎很好,不管怎样,它很简单,也不会带来太多麻烦。我仍然会尝试找到一些关于Process()的好文档,但现在这是好的。再次感谢!很高兴能帮上忙:)@RussAdams我建议退房。