| 236 | |
| 237 | # Send a ping request |
| 238 | def ping(self): |
| 239 | response_time = None |
| 240 | for retry in range(1, 3): # Retry 3 times |
| 241 | s = time.time() |
| 242 | with gevent.Timeout(10.0, False): # 10 sec timeout, don't raise exception |
| 243 | res = self.request("ping") |
| 244 | |
| 245 | if res and "body" in res and res["body"] == b"Pong!": |
| 246 | response_time = time.time() - s |
| 247 | break # All fine, exit from for loop |
| 248 | # Timeout reached or bad response |
| 249 | self.onConnectionError("Ping timeout") |
| 250 | self.connect() |
| 251 | time.sleep(1) |
| 252 | |
| 253 | if response_time: |
| 254 | self.log("Ping: %.3f" % response_time) |
| 255 | else: |
| 256 | self.log("Ping failed") |
| 257 | self.last_ping = response_time |
| 258 | return response_time |
| 259 | |
| 260 | # Request peer exchange from peer |
| 261 | def pex(self, site=None, need_num=5): |