0

So every second I am making a bunch of requests to website X every second, as of now with the standard urllib packages like so (the requestreturns a json):

import urllib.request
import threading, time

def makerequests():
    request = urllib.request.Request('http://www.X.com/Y')
    while True:
        time.sleep(0.2)
        response = urllib.request.urlopen(request)
        data = json.loads(response.read().decode('utf-8'))

for i in range(4):
    t = threading.Thread(target=makerequests)
    t.start()

However because I'm making so much requests after about 500 requests the website returns HTTPError 429: Too manyrequests. I was thinking it might help if I re-use the initial TCP connection, however I noticed it was not possible to do this with the urllib packages.

So I did some googling and discovered that the following packages might help:

  • Requests
  • http.client
  • socket ?

So I have a question: which one is best suited for my situation and can someone show an example of either one of them (for Python 3)?

3
  • 4
    To answer your question requests is (probably) the best - it handles keep alive automatically. What might actually help though is to make fewer requests. Commented Aug 20, 2015 at 11:53
  • 3
    If the website is rate limiting requests, then re-using the tcp connection probably won't work. Commented Aug 20, 2015 at 11:55
  • I assume the sysadmin of the web site knows what your are doing and why. If not looping repeatedly same request could be seen as an attack and you IP will end in a black list. Commented Aug 20, 2015 at 11:58

1 Answer 1

2

requests handles keep alive automatically if you use a session. This might not actually help you if the server is rate limiting requests, however, requests also handles parsing JSON so that's a good reason to use it. Here's an example:

import requests

s = requests.Session()
while True:
    time.sleep(0.2)
    response = s.get('http://www.X.com/y')
    data = response.json()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.