-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_video.py
executable file
·74 lines (58 loc) · 1.65 KB
/
start_video.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
69
70
71
72
73
74
#!/usr/bin/env python
import sys
import time
import socket
import netifaces as ni
host = "192.168.1.132"
port = 8000
#log outputs
def writeToLog(the_string):
f = open("/home/pi/robo-ops/pi-clients/server_listener.log", "a")
f.write(the_string + "\n")
f.close()
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
#find my ip
ni.ifaddresses('wlan0')
ip = ni.ifaddresses('wlan0')[2][0]['addr']
send(ip + "~", s)
return s
def send(a_str, s):
s.sendall(a_str)
if (len(sys.argv) < 2):
print(sys.argv[0])
print("video: server_ip")
writeToLog("video: server_ip")
sys.exit(1)
host = str(sys.argv[1])
print("video: connecting to server with ip: " + str(host) + " and port: " + str(port))
writeToLog("video: connecting to server with ip: " + str(host) + " and port: " + str(port))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
isConnected = 0
while True:
#try to connect socket
while isConnected == 0:
try:
s = connect()
isConnected = 1
print("video: connected to server")
writeToLog("video: connected to server")
except socket.error:
time.sleep(1)
s.close()
isConnected = 0
break
while True:
#try to send data
try:
data_pipe = open('/home/pi/robo-ops/pi-clients/outpipe', 'r', 0)
read = None
while sys.stdin != "":
read = data_pipe.read(1024)
send(read, s)
except socket.error:
s.close()
isConnected = 0
break
s.close()