基本上,我的想法是编写某种基本服务器,我可以连接到我的计算机,然后远程运行命令。这似乎不是一个问题;但后来我有一个明智的想法,下一步逻辑上是添加某种线程,这样我就可以产生多个连接。
我读过,因为GIL,multiprocessing.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()
答案 0 :(得分:1)
嗯,这个答案只适用于你使用unix或类似unix的操作系统(windows没有我们使用的os.fork()
)。
在unix平台上执行这些操作的最常用方法之一是fork一个新进程来处理客户端连接,同时主进程继续侦听请求。
以下是可以处理多个同时连接的简单回显服务器的代码。您只需修改handle_client_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()