我想用Python制作一个多进程UDP服务器,从类监听每个进程的一个端口:
processListener.py:
import multiprocessing
import socket
class processListener(multiprocessing.Process):
def __init__(self):
multiprocessing.Process.__init__(self)
self.data = None
def run(self):
self.startServer()
return
def startServer(self):
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
address = ('', self.port)
udpSocket.bind(address)
while 1:
data, client = udpSocket.recvfrom(1024)
print self.data, '>>>', data.strip()
self.data = data.strip()
udpSocket.sendto('ACK', client)
return
而我的主文件是server.py:
from processListener import *
# Variable Definition
port = 4000
# Sever Initialization
if __name__ == '__main__':
process = processListener()
process.port = port
process.start()
while True:
command = raw_input()
if command == 'showdata':
print 'Last Data is:', process.data
当服务器运行时,我从UDP发送数据到localhost:4000
shell$
None >>> Test Data
但是问题开始于我使用命令 showdata
shell$
None >>> Test Data
showdata
Last Data is: None
相关分类