来自不同模块的Python套接字连接
您好,我已经使用python创建了一个套接字服务器,它基本上是在运行ArchLinux作为操作系统的raspberry pi上作为后台服务工作的。套接字服务器首先绑定到IP地址和端口并等待客户端。客户端应该发送一个包含单词或命令的小字符串消息。在socket服务器中,我有一个基本的if-elif-else语句,它允许socket根据收到的命令以不同的方式作出反应。if语句将触发第二个python脚本的运行来自不同模块的Python套接字连接,python,sockets,raspberry-pi,archlinux,Python,Sockets,Raspberry Pi,Archlinux,您好,我已经使用python创建了一个套接字服务器,它基本上是在运行ArchLinux作为操作系统的raspberry pi上作为后台服务工作的。套接字服务器首先绑定到IP地址和端口并等待客户端。客户端应该发送一个包含单词或命令的小字符串消息。在socket服务器中,我有一个基本的if-elif-else语句,它允许socket根据收到的命令以不同的方式作出反应。if语句将触发第二个python脚本的运行 while True: ## Accepts in-coming connecti
while True:
## Accepts in-coming connection
conn, addr = s.accept()
## Revieves messasge from Client/Web Server
clientmsg = conn.recv(1024)
## In accordance to message recieved from the Client/Web Server the Socket Server performs a different task
## Deimmobilise vehicle
if clientmsg == "deimmobilise":
print "Client IP Address: ", addr
print "Client message: ", clientmsg
os.system("python2 /home/rpifmvehiclemodulefiles/rpifmdeimmobilisation.py")
servermsg = "ON"
print "Server message: ", servermsg
conn.send(servermsg)
clientmsg = None
servermg = None
conn.close()
## Immobilise vehicle
elif clientmsg == "immobilise":
print "Client IP Address: ", addr
print "Client message: ", clientmsg
os.system("python2 /home/rpifmvehiclemodulefiles/rpifmdeimmobilisation.py")
servermsg = "OFF"
print "Server message: ", servermsg
conn.send(servermsg)
clientmsg = None
servermg = None
conn.close()
## Emergency vehicle shut-off
elif clientmsg == "shutoff":
print "Client IP Address: ", addr
print "Client message: ", clientmsg
os.system("python2 /home/rpifmvehiclemodulefiles/rpifmemergencystop.py")
servermsg = "STOPPED"
print "Server message: ", servermsg
conn.send(servermsg)
clientmsg = None
servermg = None
conn.close()
## Capture dash-cam image
elif clientmsg == "captureimage":
print "Client IP Address: ", addr
print "Client message: ", clientmsg
os.system("python2 /home/rpifmvehiclemodulefiles/rpifmcaptureimage.py")
clientmsg = None
## Wait for Client/Web Server message
elif clientmsg == None:
time.sleep(1)
## Unrecognised Client/Web Server message
else:
print "Client IP Address: ", addr
servermg = "UNRECOGNISED VEHICLE CONTROL COMMAND"
print "Server message: ", servermsg
conn.send(servermsg)
clientmsg = None
servermg = None
conn.close()
上面是与等待客户端连接的套接字服务器相关的代码。我的问题是捕捉行车记录仪图像。如您所见,我将套接字连接保持打开状态,以便稍后在由命令触发的脚本中使用。运行第二个脚本后,它应通过现有套接字连接发送base64编码的映像。这是图像捕获代码
#!/usr/bin/env python
## Import Python Libraries
import time ## Time control function Library
import picamera ## Pi Camera module Library
import base64 ## Binary encoding Library
import socket ## Socket related Library
from rpifmsocketserver import conn ## Socket server settings
with picamera.PiCamera() as camera:
camera.resolution = (640, 480) ##Image Size
camera.start_preview() ## Start camera
time.sleep(2) ## Wait for camera to start up correctly
camera.capture('/home/rpifmvehiclemodulefiles/rpifmdashcamimages/SJA209.jpg') ## Location, name and format of image
## Select captured dash-cam image
jpgimage = '/home/rpifmvehiclemodulefiles/rpifmdashcamimages/SJA209.jpg'
## Convert image to a string of base64 encoded binary
jpgtext = 'jpg1_b64 = \\\n"""' + base64.encodestring(open(jpgimage,"rb").read()) + '"""'
## Outputs produced string; for testing purposes only
print jpgtext
## Send string over existing socket connection
conn.send(jpgtext)
conn.close()
我假设我的问题是,现有套接字连接的详细信息在第二个脚本中没有得到正确的恢复…如何使用现有的套接字连接:)谢谢 考虑使用
zmq
。处理socket总是很困难的,必须非常小心地走,并预测各种场景。使用zmq
您可以将其留给软件包,集中精力实现真正的逻辑
我以前的StackOverflow回答显示,这是一个能够通过网络为客户服务的完整工作示例。当然,速度很快
使用zmq的其他示例应用程序有iPython、SaltStack和其他。你可以从中学到很多东西
我会把zmq美女应用到你的处境中