jwang
jwang

Reputation: 25

urllib2 works where request doesn't

EDIT: Solved! See below.

I'm a recent convert to using the requests library for python. However, I'm encountering a situation where urllib2 works and requests doesn't.

import requests, urllib2
url = "http://thepiratebay.se/search/test/0/7/0"
print urllib2.urlopen(url).getcode() # 200 - OK!
print requests.get(url).status_code # 400 :(

I've also tried setting headers but it doesn't seem to fix the problem, so I've included my minimal example. Other thoughts: is urllib2 somehow handling the redirect to thepiratebay.sx correctly while requests doesn't?

Thanks in advance!


Thanks for the help all, I upgraded requests (1.0.4 => 1.2.2) and it solved the problem. Oops, should have considered that earlier!

Upvotes: 2

Views: 171

Answers (2)

Phillip Nordwall
Phillip Nordwall

Reputation: 465

Both of these should handle redirects by default

From http://docs.python.org/2/library/urllib2.html, urllib2 handles "basic and digest authentication, redirections, cookies and more."

From http://docs.python-requests.org/en/latest/user/quickstart.html#redirection-and-history "Requests will automatically perform location redirection while using the GET and OPTIONS verbs."

Both of these return a 200 response for me.

Upvotes: 0

computmaxer
computmaxer

Reputation: 1713

The requests library does handle redirects by default, as seen here.

In fact, I cannot reproduce your issue: enter image description here

Perhaps try it again?

Upvotes: 1

Related Questions