-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTimeServiceTimeOut.py
97 lines (90 loc) · 2.26 KB
/
TimeServiceTimeOut.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
# Copyright (c) 2011, Yaler GmbH, Switzerland
# All rights reserved
import sys
import time
import socket
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 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):
x = [0] * 3
while True:
acceptable=False
print('opening socket at '+time.strftime('%H:%M:%S', time.localtime()))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(75)
try:
s.connect((host, port))
except socket.error:
print('Couldn''t connect at '+time.strftime('%H:%M:%S', time.localtime()))
s.close()
s=None
break
try:
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)
if not acceptable or (x[0] != '2') or (x[1] != '0') or (x[2] != '4'):
break
except socket.timeout:
print('Timeout. Sleeping at '+time.strftime('%H:%M:%S', time.localtime()))
s.close()
time.sleep(1)
print('Has slept')
print('')
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
while True:
s = accept(sys.argv[1], 80, sys.argv[2])
if s is not None:
s.send(
'HTTP/1.1 200 OK\r\n'
'Connection: close\r\n'
'Content-Length: 8\r\n\r\n' +
time.strftime('%H:%M:%S', time.localtime()))
time.sleep(0.001)
s.close()
print('Running')