Tuesday, May 12, 2015

Python socket server to send camera image to client (II)

Last exercise implement Python socket server to send camera image to client, wait client, send image file, and exit. In this exercise, the server will stay in the program and wait another connection from client, without exit.


pyCamJpgServer2.py
import io
import socket
import picamera
import atexit

def camServer():

    while True:
        print("wait...")
        conn, addr = server_socket.accept()
        if conn:
            print(conn)
            print(addr)
            connection = conn.makefile('wb')
            break

    print("Connecting")
    try:
        stream = io.BytesIO()
        camera.capture(stream, 'jpeg')
        stream.seek(0)
        connection.write(stream.read())
        stream.seek(0)
        stream.truncate()
    finally:
        print("close connection")
        connection.close()

def onExit():
    connection.close()
    server_socket.close()
    print("exit")

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_preview()
    atexit.register(onExit)

    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 8000))
    server_socket.listen(0)
    server_socket.setblocking(1)
    
    while True:
        camServer()


No comments: