繁体   English   中英

来自不同模块的Python套接字连接

[英]Python socket connection from different module

嗨,我已经使用python创建了一个套接字服务器,基本上它在运行arch linux作为操作系统的树莓派上用作后台服务。 套接字服务器首先绑定到IP地址和端口,然后等待客户端。 客户端应发送一条包含单词或命令的小字符串消息。 在套接字服务器中,我有一个基本的if if else语句,该语句将允许套接字根据收到的命令以不同的方式做出反应。 if语句将触发第二个python脚本的运行。

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 处理套接字总是很困难,必须非常小心地行走并预测各种情况。 使用zmq您可以将其留在程序包中,并专注于要实现的实际逻辑。

我以前的StackOverflow答案显示了如何实现可通过TCP套接字访问的更衣室服务器,这是一个完整的工作示例,能够通过网络为客户端提供服务。 当然,这是非常快的。

使用zmq的其他示例应用程序是iPython,SaltStack等。 您可以从pyzmq示例目录中学到很多东西。

我将把zmq美女应用于您的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM