0

Hi I have created a socket server using python, basically it works as a background service on a raspberry pi running arch linux as an OS. The socket server first binds to an IP address and port and waits for a client. the client should send a small string message containing a word or command. Within the socket server i have a basic if elif else statement that will allow the socket to react in a different way in accordance to the received command. The if statement will trigger the running of a second python script.

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()

Above is the code related to the socket server waiting for a client to connect. the problem I have is with the capture dash-cam image. as you can see I am leaving the socket connection open to be later used in the script that is triggered by the command. Once the second script is run it should send off the base64 encoded image over the existing socket connection. Here is the image capture code

#!/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()

I assume my problem is that the details of the existing socket connection are not being retreved correctly in the second script...how can I use the existing socket connection :) thanks!

1 Answers1

0

Consider using zmq. Dealing with socket is always difficult, one has to walk very carefully and predict various scenarios. With zmq you can leave this to the package and concentrate on real logic you want to implement.

My older StackOverflow answer shows how to implement locker server accessible over TCP sockets is full working example being able serving clients over network. Be sure, it is very fast.

Other sample applications using zmq are iPython, SaltStack and others. You may learn a lot from pyzmq example directory.

I will leave applying zmq beauties into your situation to you.

Community
  • 1
  • 1
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98