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

时间:2014-04-25 15:03:58

标签: python sockets raspberry-pi archlinux

您好我已经使用python创建了一个套接字服务器,基本上它作为操作系统运行arch linux的raspberry pi上的后台服务。套接字服务器首先绑定到IP地址和端口,然后等待客户端。客户端应发送包含单词或命令的小字符串消息。在套接字服务器中,我有一个基本的if ifif 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()

我认为我的问题是在第二个脚本中没有正确地恢复现有套接字连接的细节...我怎样才能使用现有的套接字连接:)谢谢!

1 个答案:

答案 0 :(得分:0)

考虑使用zmq。处理套接字总是很困难,必须非常小心地行走并预测各种情况。使用zmq,您可以将其留给包,并专注于您想要实现的实际逻辑。

我早期的StackOverflow答案显示how to implement locker server accessible over TCP sockets是完整的工作示例,可以通过网络为客户提供服务。确定,它非常快。

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

我将把zmq美女留给你的情况。