请教使用Python的同志们,
我做研究需要,我想请教大家一个问题使用python作为TCP server,同时接收两个clients传来的数据的问题。 我希望同步驾驶模拟器 (运行Fedora Realtime operating system)和手机的时间戳。
我把服务器电脑 (运行windows 7), 驾驶模拟器电脑(运行fedora)和iphone接入了同一个网络。 然后运行下面的代码。 (代码可以在下面的pastebin.com下载)。我可以通过iphone下的SoketTester 程序用5050端口把手机上的信息传到服务器电脑中。也可以把驾驶模拟器中的数据传到服务器的电脑中。
因为我期望同步手机和驾驶模拟器的时间戳, 所以我期望同时把驾驶模拟器和手机的数据传到服务器的电脑中。 因此,我想通过多线程,多进程 或者其它方式来实现。我在Socketpython.py的基础上写出了TwoSockets.py。代码在下面和pastbin中都有。 我不知道如何实现两个clients同时向一个TCP server传数据。 我的猜想和研究是1. 同一个ip同一个port (5050) server可以同时接收两个clients的数据; 2. 同一个ip,不同的ports(如5050和5051)server可以同时接收两个clients的数据。 3. 我的服务器电脑也有两个ip,也许同一台电脑的不同ip,不同ports的server可以同时接收两个clients的数据。我这三种猜想都试了。不知道哪一个正确。可是我的TwoSockets.py的代码都不法做到用python 作为TCP server,接收两个clients传来的数据。
请大家指点如何用python 作为TCP server,接收两个clients传来的数据。谢谢!
如果这个问题比较麻烦,我愿意付钱。如果收钱,请报个价。 谢谢。
-----------------------------------------------------------代码1. Socketpython.py---------------------------------------------
http://pastebin.com/n49gKeq4
'''
Socketpython.py
Simple socket server using threads
'''
import socket
import sys
socket.setdefaulttimeout(60)
HOST = '192.168.10.135' # Symbolic name, meaning all available interfaces
PORT = 5050 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
conn, addr = s.accept()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
#s.listen(10)
#print 'start loop'
data= conn.recv(1024)
print data
if len(data)==0:break
#conn, addr = s.accept()
## print 'Connected with ' + addr[0] + ':' + str(addr[1])
## print conn
print '-_-!'
s.close()
-----------------------------------------------------------代码1. TwoSockets.py---------------------------------------------
http://pastebin.com/FLW9QRcm
'''
Simple socket server using threads
'''
import socket
import sys
import threading
from threading import Thread
def OpenSocketPort1(HOST,PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
conn, addr = s.accept()
outfile =open('text1.txt','a')
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
#s.listen(10)
#print 'start loop'
data= conn.recv(1024)
print data
print >>outfile,data
if len(data)==0:break
#conn, addr = s.accept()
## print 'Connected with ' + addr[0] + ':' + str(addr[1])
## print conn
print '-_-!'
s.close()
outfile.close()
def OpenSocketPort2(HOST,PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
conn, addr = s.accept()
outfile =open('text1.txt','a')
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
#s.listen(10)
#print 'start loop'
data= conn.recv(1024)
print data
print >>outfile,data
if len(data)==0:break
#conn, addr = s.accept()
## print 'Connected with ' + addr[0] + ':' + str(addr[1])
## print conn
print '-_-!'
s.close()
outfile.close()
if __name__ == '__main__':
#socket.setdefaulttimeout(60)
HOST1 = '192.168.10.135' # Symbolic name, meaning all available interfaces
HOST2 = '192.168.10.2'
PORT1 = 5050 # Arbitrary non-privileged port
PORT2 = 5051
print '1'
from multiprocessing import Pool
pool = Pool()
result1 = pool.apply_async(OpenSocketPort1,[HOST1,PORT1]) # evaluate "solve1(A)" asynchronously
print '2'
#result2 = pool.apply_async(OpenSocketPort2,[HOST2,PORT2]) # evaluate "solve2(B)" asynchronously
print '3'
answer1 = result1.get(timeout=30)
print '4'
#answer2 = result2.get(timeout=30)
print '5'
print answer1
print answer2
#OpenSocketPort(HOST,PORT)
---------------------------
He Jibo
Assistant Professor
Department of Psychology,
Wichita State University
website: www.hejibo.info