forked from philterphactory/skypebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hookserver.py
90 lines (72 loc) · 2.42 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
89
90
import SocketServer
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import queuedthread
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, portnumber ):
SocketServer.TCPServer.__init__( self, ("", portnumber), HookRequestHandler )
def handle_request( self ):
self._payload = None
SocketServer.TCPServer.handle_request( self )
class HookServerThread( queuedthread.QueuedThread ):
@property
def portnumber(self):
return self._portnumber
@portnumber.setter
def portnumber(self, value):
self._portnumber = value
def stop( self ):
if self.httpd:
self.httpd.timeout = 1
super(HookServerThread,self).stop()
def run( self ):
self._abortflag = False
self.httpd = HookHTTPServer( portnumber=self._portnumber )
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:
logging.info( "HookServer: found an http payload")
message = HookServerMessage( HookServerMessage.RECIEVED_PUSH, self.httpd.payload )
self.put_message( message )
self.httpd.payload = None
self.httpd.socket.close()