pybitflyer
pybitflyer copied to clipboard
Add an option for HTTP Keep-Alive
To enable HTTP keep-alive over multiple requests, I would like to propose this pull request (including some minor changes).
Thank you for your pull req. I have to consider carefully whether merge of not, because all pybitflyer users will have to change their codes. Please give me a little time to think about this.
I have been using this PR for about 3 months, but it does not interfere.
HTTP keep-alive over multiple requests can be confirmed with the following code.
import pybitflyer
import logging
import time
KEEP_SESSION=True
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s")
api = pybitflyer.API(keep_session=KEEP_SESSION)
while True:
api.ticker()
time.sleep(1)
KEEP_SESSION=False
2018-04-02 08:40:53,441 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:53,531 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:54,541 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:54,626 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:55,635 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:55,732 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:56,740 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:56,829 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:57,837 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:57,933 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
KEEP_SESSION=True
2018-04-02 08:41:17,315 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:41:17,400 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:18,432 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:19,466 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:20,507 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:21,537 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:22,572 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:23,602 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:24,632 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:25,661 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:26,691 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
In this case, the HTTP connection is reset every 100 seconds. It is probably due to HTTP server configuration of Cloudflare(CDN of API server).
2018-04-02 08:38:29,996 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:38:31,004 Resetting dropped connection: api.bitflyer.jp
2018-04-02 08:38:31,094 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:38:32,133 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None