Revision f22c1cea
b/lib/http.py | ||
---|---|---|
160 | 160 |
return serializer.LoadJson(data) |
161 | 161 |
|
162 | 162 |
|
163 |
def WaitForSocketCondition(sock, poller, event, timeout): |
|
164 |
"""Waits for a condition to occur on the socket. |
|
165 |
|
|
166 |
@type sock: socket |
|
167 |
@param socket: Wait for events on this socket |
|
168 |
@type poller: select.Poller |
|
169 |
@param poller: Poller object as created by select.poll() |
|
170 |
@type event: int |
|
171 |
@param event: ORed condition (see select module) |
|
172 |
@type timeout: float or None |
|
173 |
@param timeout: Timeout in seconds |
|
174 |
@rtype: int or None |
|
175 |
@return: None for timeout, otherwise occured conditions |
|
176 |
|
|
177 |
""" |
|
178 |
check = (event | select.POLLPRI | |
|
179 |
select.POLLNVAL | select.POLLHUP | select.POLLERR) |
|
180 |
|
|
181 |
if timeout is not None: |
|
182 |
# Poller object expects milliseconds |
|
183 |
timeout *= 1000 |
|
184 |
|
|
185 |
poller.register(sock, event) |
|
186 |
try: |
|
187 |
while True: |
|
188 |
# TODO: If the main thread receives a signal and we have no timeout, we |
|
189 |
# could wait forever. This should check a global "quit" flag or |
|
190 |
# something every so often. |
|
191 |
io_events = poller.poll(timeout) |
|
192 |
if not io_events: |
|
193 |
# Timeout |
|
194 |
return None |
|
195 |
for (evfd, evcond) in io_events: |
|
196 |
if evcond & check: |
|
197 |
return evcond |
|
198 |
finally: |
|
199 |
poller.unregister(sock) |
|
200 |
|
|
201 |
|
|
163 | 202 |
class HttpSslParams(object): |
164 | 203 |
"""Data class for SSL key and certificate. |
165 | 204 |
|
... | ... | |
1006 | 1045 |
|
1007 | 1046 |
return buf |
1008 | 1047 |
|
1009 |
def _WaitForCondition(self, event, timeout): |
|
1010 |
"""Waits for a condition to occur on the socket. |
|
1011 |
|
|
1012 |
@type event: int |
|
1013 |
@param event: ORed condition (see select module) |
|
1014 |
@type timeout: float or None |
|
1015 |
@param timeout: Timeout in seconds |
|
1016 |
@rtype: int or None |
|
1017 |
@return: None for timeout, otherwise occured conditions |
|
1018 |
|
|
1019 |
""" |
|
1020 |
check = (event | select.POLLPRI | |
|
1021 |
select.POLLNVAL | select.POLLHUP | select.POLLERR) |
|
1022 |
|
|
1023 |
if timeout is not None: |
|
1024 |
# Poller object expects milliseconds |
|
1025 |
timeout *= 1000 |
|
1026 |
|
|
1027 |
self.poller.register(self.sock, event) |
|
1028 |
try: |
|
1029 |
while True: |
|
1030 |
# TODO: If the main thread receives a signal and we have no timeout, we |
|
1031 |
# could wait forever. This should check a global "quit" flag or |
|
1032 |
# something every so often. |
|
1033 |
io_events = self.poller.poll(timeout) |
|
1034 |
if io_events: |
|
1035 |
for (evfd, evcond) in io_events: |
|
1036 |
if evcond & check: |
|
1037 |
return evcond |
|
1038 |
else: |
|
1039 |
# Timeout |
|
1040 |
return None |
|
1041 |
finally: |
|
1042 |
self.poller.unregister(self.sock) |
|
1043 |
|
|
1044 | 1048 |
def _SocketOperation(self, op, arg1, error_msg, timeout_msg): |
1045 | 1049 |
"""Wrapper around socket functions. |
1046 | 1050 |
|
... | ... | |
1084 | 1088 |
else: |
1085 | 1089 |
wait_for_event = event_poll |
1086 | 1090 |
|
1087 |
event = self._WaitForCondition(wait_for_event, timeout) |
|
1091 |
event = WaitForSocketCondition(self.sock, self.poller, wait_for_event, |
|
1092 |
timeout) |
|
1088 | 1093 |
if event is None: |
1089 | 1094 |
raise _HttpClientTimeout(timeout_msg) |
1090 | 1095 |
|
... | ... | |
1180 | 1185 |
|
1181 | 1186 |
if not connected: |
1182 | 1187 |
# Wait for connection |
1183 |
event = self._WaitForCondition(select.POLLOUT, self.CONNECT_TIMEOUT) |
|
1188 |
event = WaitForSocketCondition(self.sock, self.poller, |
|
1189 |
select.POLLOUT, self.CONNECT_TIMEOUT) |
|
1184 | 1190 |
if event is None: |
1185 | 1191 |
raise _HttpClientError("Timeout while connecting to server") |
1186 | 1192 |
|
Also available in: Unified diff