Show More
@@ -24,6 +24,7 b' import logging' | |||||
24 | import tempfile |
|
24 | import tempfile | |
25 | import traceback |
|
25 | import traceback | |
26 | import threading |
|
26 | import threading | |
|
27 | import socket | |||
27 |
|
28 | |||
28 | from BaseHTTPServer import BaseHTTPRequestHandler |
|
29 | from BaseHTTPServer import BaseHTTPRequestHandler | |
29 | from SocketServer import TCPServer |
|
30 | from SocketServer import TCPServer | |
@@ -152,20 +153,34 b' class HttpHooksCallbackDaemon(ThreadedHo' | |||||
152 | # request and wastes cpu at all other times. |
|
153 | # request and wastes cpu at all other times. | |
153 | POLL_INTERVAL = 0.01 |
|
154 | POLL_INTERVAL = 0.01 | |
154 |
|
155 | |||
|
156 | def get_available_port(): | |||
|
157 | family = socket.AF_INET | |||
|
158 | socktype = socket.SOCK_STREAM | |||
|
159 | host = '127.0.0.1' | |||
|
160 | ||||
|
161 | mysocket = socket.socket(family, socktype) | |||
|
162 | mysocket.bind((host, 0)) | |||
|
163 | port = mysocket.getsockname()[1] | |||
|
164 | mysocket.close() | |||
|
165 | del mysocket | |||
|
166 | return port | |||
|
167 | ||||
155 | def _prepare(self, txn_id=None, host=None, port=None): |
|
168 | def _prepare(self, txn_id=None, host=None, port=None): | |
156 |
|
169 | |||
157 | host = host or '127.0.0.1' |
|
170 | host = host or '127.0.0.1' | |
158 | self._done = False |
|
171 | port = port or self.get_available_port() | |
|
172 | server_address = (host, port) | |||
159 | self.hooks_uri = '{}:{}'.format(host, port) |
|
173 | self.hooks_uri = '{}:{}'.format(host, port) | |
160 | self.txn_id = txn_id |
|
174 | self.txn_id = txn_id | |
|
175 | self._done = False | |||
|
176 | ||||
|
177 | log.debug( | |||
|
178 | "Preparing HTTP callback daemon at `%s` and registering hook object: %s", | |||
|
179 | self.hooks_uri, HooksHttpHandler) | |||
|
180 | ||||
|
181 | self._daemon = TCPServer(server_address, HooksHttpHandler) | |||
161 | # inject transaction_id for later verification |
|
182 | # inject transaction_id for later verification | |
162 | self._daemon.txn_id = self.txn_id |
|
183 | self._daemon.txn_id = self.txn_id | |
163 | log.debug( |
|
|||
164 | "Preparing HTTP callback daemon at `%s` and registering hook object", |
|
|||
165 | self.hooks_uri) |
|
|||
166 |
|
||||
167 | self._daemon = TCPServer((host, port or 0), HooksHttpHandler) |
|
|||
168 | _, port = self._daemon.server_address |
|
|||
169 |
|
184 | |||
170 | def _run(self): |
|
185 | def _run(self): | |
171 | log.debug("Running event loop of callback daemon in background thread") |
|
186 | log.debug("Running event loop of callback daemon in background thread") |
General Comments 0
You need to be logged in to leave comments.
Login now