-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
68 lines (52 loc) · 1.47 KB
/
server.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import socket
import threading
import time
import numpy as np
import cv2
from io import BytesIO
import argparse
def display_image(bytes):
frame = cv2.imdecode(np.fromstring(bytes, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
'''
####################
Do anything you want to do with the frame recvd
####################
'''
cv2.imshow("streamera", frame)
cv2.waitKey(1)
def main(server, port):
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((server, port))
print(f'[+] streamera server created at {server}:{port}')
except:
print(f'[!] Could not create streamera server at {server}:{port}')
return -1
server_socket.listen(5)
cli_sock, ret = server_socket.accept()
print(f"[+] CLIENT JOINED: {cli_sock}")
SIZE = 1500
str_buf = b''
while True:
try:
cli_sock.sendall(b'asdf')
msg = cli_sock.recv(SIZE)
except:
print('[-] Client disconnected.\n[-] Terminating server.')
break
if(msg):
str_buf += msg
# if string contains ffd9 (it means end of image)
pos = str_buf.find(b'\xff\xd9')
if pos >= 0:
# call display_image()
display_image(str_buf[:pos+2])
# replace str_buf with left over bytes
str_buf = str_buf[pos+2:]
server_socket.close()
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("server", type=str, help="IP Address")
argparser.add_argument("port", type=int, help="Port")
args = argparser.parse_args()
main(args.server, args.port)