Sun, 30 Nov 2014 03:55:47 +0200
- add more decimals to the mathematical constants
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | import ssl |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | import socket |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | import errno |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | import select |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | import asyncore |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | import base64 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | import re |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | import json |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | import urllib |
94 | 10 | import irc as Irc |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | from datetime import datetime, timedelta |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | from configfile import Config |
97 | 13 | import hgpoll as HgPoll |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | g_credentials = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | g_portnumber = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | g_throttle = [] |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | valid_repos = ['Torr_Samaho/zandronum', 'Torr_Samaho/zandronum-stable', |
97 | 20 | 'crimsondusk/zandronum-sandbox', 'crimsondusk/zandronum-sandbox-stable'] |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | def is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | i = 0 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | while i < len (g_throttle): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | if g_throttle[i][1] <= datetime.utcnow(): |
94 | 27 | print 'Throttle of %s expired' % g_throttle[i][0] |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | item = g_throttle.pop (i) # expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | |
94 | 30 | if item[0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | return False # this address expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
94 | 35 | if g_throttle[i][0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | return True # is throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | i += 1 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | return False # not throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | def throttle (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | tt = datetime.utcnow() + timedelta (0, 30) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | # attempt to just update the item |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | g_throttle[g_throttle.index (address)][1] = tt |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | except ValueError: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | # not in list |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | g_throttle.append ([address, tt]) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | |
96 | 52 | Irc.broadcast ('Throttling %s for 30 seconds' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | def handle_rest_http (data, address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | global g_credentials |
94 | 56 | displayaddress = address |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | authrex = re.compile (r'^authorization: Basic (.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | payloadrex = re.compile (r'^payload=(.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | authenticated = False |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | payload = '' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | if not g_credentials: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | g_credentials = base64.b64encode (Config.get_node ('rest').get_value ('credentials')) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | # Authenticate and find the payload |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | for line in data: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | match = authrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | |
94 | 69 | if match and match.group (1) == g_credentials: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | authenticated = True |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | match = payloadrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | if match: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | payload = match.group (1) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | if not authenticated: |
96 | 78 | Irc.broadcast ('%s failed to authenticate' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | jsonstring = urllib.unquote_plus (payload).decode ('utf-8') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | jsondata = json.loads (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | repodata = jsondata['repository'] |
99 | 87 | repo_fullname = '%s/%s' % (repodata['owner'], repodata['name']) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
99 | 89 | if repo_fullname not in valid_repos: |
90 | raise ValueError ('unknown repository %s' % repo_fullname) | |
97 | 91 | |
92 | commit_data = [] | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | if 'commits' in jsondata: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | for commit in jsondata['commits']: |
97 | 96 | commit_data.append ([commit['node'], commit['message']]) |
97 | ||
98 | if len (commit_data) > 0: | |
99 | 99 | HgPoll.process_new_commits (repodata['name'], commit_data) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | except Exception as e: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | Irc.broadcast ('%s provided bad JSON: %s' % (address, e)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | with open ('rejected_json.txt', 'w') as fp: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | fp.write (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | Irc.broadcast ('bad json written to rejected_json.txt') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | except: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | Irc.broadcast ('failed to log json') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | pass |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | class RESTConnection (asyncore.dispatcher): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | httpdata = '' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | writebuffer = '' |
94 | 117 | address = None |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | def __init__ (self, conn, address): |
96 | 120 | asyncore.dispatcher.__init__ (self, conn) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | self.socket = ssl.wrap_socket (conn, server_side=True, keyfile='key.pem', |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | certfile='cert.pem', do_handshake_on_connect=False) |
96 | 123 | self.socket.settimeout (3) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | self.address = address |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | while True: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | self.socket.do_handshake() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | break |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | except ssl.SSLError, err: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | select.select([self.socket], [], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | select.select([], [self.socket], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | else: |
94 | 136 | Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err)) |
137 | throttle (self.address[0]) | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | def write (self, msg): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | self.writebuffer += msg |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | def readable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | return True |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | def writable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | return self.writebuffer |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | def handle_close (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
153 | def handle_read (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | while 1: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | data = self.recv (4096) |
94 | 157 | except: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | # EOF |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | self.httpdata += data.replace ('\r', '') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | def finish (self): |
94 | 165 | handle_rest_http (self.httpdata.split ('\n'), self.address[0]) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | def handle_write (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | self.send (self.writebuffer) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
170 | self.writebuffer='' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
171 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
173 | raise |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
174 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | class RESTServer (asyncore.dispatcher): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | def __init__ (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
177 | global g_portnumber |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | if g_portnumber == None: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
180 | g_portnumber = Config.get_node ('rest').get_value ('port') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
181 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
182 | asyncore.dispatcher.__init__ (self) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | self.create_socket (socket.AF_INET, socket.SOCK_STREAM) |
94 | 184 | self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | self.bind (('', g_portnumber)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | self.listen (5) |
93 | 187 | print 'REST server initialized' |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | def handle_accept (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | sock, address = self.accept() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | if is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | # throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | sock.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | |
94 | 197 | conn = RESTConnection (sock, address) |
198 | ||
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | return |