我在第一台机器上有3个程序( clientA.py,ClientB.py,Server_X.py ),在第2台机器上有1个程序( machine_Y.py )。<登记/>
我需要作为图片连接并同时从两个客户端发送数据到主机Y上的服务器,修改此数据并发送到主机X中的服务器。
客户A.
import socket
import time
import threading
def client_cam():
# create socket and bind host
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 9000))
connection = client_socket.makefile('wb')
try:
count=1
while True:
data = 'from CAMERA: '
client_socket.send(b'Camera')
time.sleep(1)
count+=1
finally:
connection.close()
client_socket.close()
if __name__ == '__main__':
client_cam()
除了端口号
之外,客户B与上述相同 machine_Y.py....# here some handlers for VideoStreamHandler , SensorDataHandler
class ThreadServer(object):
def server_thread(host, port):
server = SocketServer.TCPServer((host, port), VideoStreamHandler)
server.serve_forever()
def server_thread2(host, port):
server = SocketServer.TCPServer((host, port), SensorDataHandler)
server.serve_forever()
def motor_client(host,port):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
connection = client_socket.makefile('wb')
try:
count = 1
while True:
data = 'from Motor: '
client_socket.send(b'motor')
time.sleep(1)
count += 1
finally:
connection.close()
client_socket.close()
motor_thread = threading.Thread(target=motor_client('localhost',9003))
video_thread = threading.Thread(target=server_thread('localhost', 9000))
distance_thread = threading.Thread(target=server_thread2('localhost', 9001))
video_thread.start()
distance_thread.start()
motor_thread.start()
if __name__ == '__main__':
ThreadServer()
Server_X.py
import SocketServer
import threading
class MotorStreamHandler(SocketServer.BaseRequestHandler):
data = " "
def handle(self):
try:
while self.data:
self.data = self.request.recv(1024)
print(self.data)
finally:
print("Connection closed on thread Motor")
class ThreadServer(object):
def server_thread(host, port):
server = SocketServer.TCPServer((host, port), MotorStreamHandler)
server.serve_forever()
motor_thread = threading.Thread(target=server_thread('localhost',9003))
motor_thread.start()
if __name__ == '__main__':
ThreadServer()