-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpycam2.py
45 lines (38 loc) · 965 Bytes
/
pycam2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()