-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyunYalerNoTimeOut.py
executable file
·145 lines (122 loc) · 3.42 KB
/
yunYalerNoTimeOut.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) 2011, Yaler GmbH, Switzerland
# All rights reserved
# based on TimeService.py by Yaler GmbH
# modified for Arduino Yun by Bo Peterson www.asynkronix.se
import sys
import time
import socket
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
def find (pattern, s):
x = [0] * len(pattern)
i = j = t = 0
while True:
k = 0
match = True
while (k != len(pattern)) and match:
if i + k == j:
x[j % len(x)] = s.recv(1)
j += 1
t = x[(i + k) % len(x)]
match = pattern[k] == t
k += 1
i += 1
if match or (t == ''):
break
return match
def getStringBeforePattern (pattern, s):
beforePattern=''
x = [0] * len(pattern)
i = j = t = 0
while True:
k = 0
match = True
while (k != len(pattern)) and match:
if i + k == j:
c=s.recv(1)
beforePattern += c
x[j % len(x)] = c
j += 1
t = x[(i + k) % len(x)]
match = pattern[k] == t
k += 1
i += 1
if match or (t == ''):
break
if match:
beforePattern=beforePattern[:(len(beforePattern)-len(pattern))]
return match,beforePattern
def location(s):
host = ''
port = 80
if find('\r\nLocation: http://', s):
x = s.recv(1)
while (x != '') and (x != ':') and (x != '/'):
host += x
x = s.recv(1)
if x == ':':
port = 0
x = s.recv(1)
while (x != '') and (x != '/'):
port = 10 * port + ord(x) - ord('0')
x = s.recv(1)
return host, port
def accept(host, port, id, usebridge):
rest='{"command":"unsupported","action":"unsupported"}' # if /arduino not found in url
putStatus=False
x = [0] * 3
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
while True:
s.send(
'POST /' + id + ' HTTP/1.1\r\n'
'Upgrade: PTTH/1.0\r\n'
'Connection: Upgrade\r\n'
'Host: ' + host + '\r\n\r\n')
j = 0
while j != 12:
x[j % 3] = s.recv(1)
j += 1
if (x[0] == '3') and (x[1] == '0') and (x[2] == '7'):
host, port = location(s)
acceptable = find('\r\n\r\n', s)
#find the calling url and parse it
if acceptable and (x[0] == '1') and (x[1] == '0') and (x[2] == '1'):
keepGoing, requestUrl = getStringBeforePattern(' HTTP/1.1', s)
pos=requestUrl.lower().find('/arduino')
if pos!=-1:
rest=requestUrl.lower()[pos+8:]
if usebridge:
value.put('rest',rest)
now=time.strftime('%H:%M:%S', time.localtime())
value.put('time',now)
putStatus=True
else:
print rest
if not acceptable or (x[0] != '2') or (x[1] != '0') or (x[2] != '4'):
break
if not acceptable or (x[0] != '1') or (x[1] != '0') or (x[2] != '1'):
s.close()
s = None
if not acceptable or (x[0] != '3') or (x[1] != '0') or (x[2] != '7'):
break
return s, rest, putStatus
usebridge=False
if len(sys.argv)>3:
if sys.argv[3]=='bridge':
usebridge=True
while True:
s,rest,putStatus = accept(sys.argv[1], 80, sys.argv[2],usebridge)
time.sleep(0.3) # this should be about 50% longer than interval in arduino sketch
if putStatus:
reply=value.get('answer')
else:
reply=rest # url must contain /arduino
s.send(
'HTTP/1.1 200 OK\r\n'
'Connection: close\r\n'
'Content-Length: '+str(len(reply))+'\r\n\r\n' + reply)
time.sleep(0.001)
s.close()