forked from philterphactory/skypebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hookserver.py
88 lines (70 loc) · 1.82 KB
/
hookserver.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
import SocketServer
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import threading
import cgi
import json
import logging
class HookRequestHandler( BaseHTTPRequestHandler ):
def do_GET(self):
self.send_response(200)
self.end_headers()
return
def do_POST(self):
fields = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
payload_in = fields['payload'].value
print payload_in
payload = None
try:
payload = json.loads( payload_in )
except Exception:
pass
self.server.payload = payload
self.send_response(200)
self.end_headers()
class HookServerMessage( object ):
RECIEVED_PUSH = 0
def __init__( self, code, payload ):
self.code = code
self.payload = payload
class HookHTTPServer( SocketServer.TCPServer ):
@property
def payload(self):
return self._payload
@payload.setter
def payload(self, value):
self._payload = value
def __init__( self ):
SocketServer.TCPServer.__init__( self, ("", 31337), HookRequestHandler )
def handle_request( self ):
self._payload = None
SocketServer.TCPServer.handle_request( self )
class HookServerThread( threading.Thread ):
@property
def queue(self):
return self._queue
@queue.setter
def queue(self, value):
self._queue = value
def stop( self ):
self._abortflag = True
if self.httpd:
self.httpd.timeout = 1
def run( self ):
self._abortflag = False
self.httpd = HookHTTPServer()
self.httpd.timeout = 5
while not self._abortflag:
try:
self.httpd.handle_request()
except Exception, e:
logging.info( e )
pass
if self.httpd.payload is not None:
message = HookServerMessage( HookServerMessage.RECIEVED_PUSH, self.httpd.payload )
self.queue.put( message )
self.httpd.socket.close()