Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a difference between curl -u and python requests

So there's a web page that I want to retreive with python.Requests

https://ororo.tv/api/v2/episodes/9

which requires basic authentication. If I do it with curl like this

 curl -u [email protected]:password https://ororo.tv/api/v2/episodes/9

I get the response I want, however, when trying to do the same in python with Requests library, like this

>>> r = requests.get('https://ororo.tv/api/v2/episodes/9', auth=('[email protected]', 'password'))
>>> r
<Response [520]>

I always get 520 response. Could someone tell me, what i could be doing wrong ?

like image 856
Виталик Бушаев Avatar asked Feb 08 '17 13:02

Виталик Бушаев


1 Answers

Yes, there are subtle differences. There are slight differences in the headers being sent, and those apparently matter to this API.

If you change the URL queried to use http://httpbin.org/get (an end-point of the online HTTP test service HTTPBin.org, you can see the differences in what curl and requests send:

$ curl -u [email protected]:password http://httpbin.org/get
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTpwYXNzd29yZA==",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.51.0"
  },
  "origin": "84.92.98.170",
  "url": "http://httpbin.org/get"
}
$ python -c "import requests; print(requests.get('http://httpbin.org/get', auth=('[email protected]', 'password')).text)"
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTpwYXNzd29yZA==",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.11.1"
  },
  "origin": "84.92.98.170",
  "url": "http://httpbin.org/get"
}

To highlight the differences:

  • requests sends one extra header, Accept-Encoding, set to gzip, deflate
  • The User-Agent header differs; both reflect the current agent.

You'd have to see which one of these headers causes the issue on the https://ororo.tv/api/v2 site. When I correct the URL to use v2 and https, like the curl command, and set the User-Agent header then I get a valid response:

>>> headers = {'User-Agent': 'curl/7.51.0'}
>>> r = requests.get('https://ororo.tv/api/v1/episodes/9',
                     auth=('[email protected]', 'password'), headers=headers)
>>> r
<Response [200]>
>>> from pprint import pprint
>>> pprint(r.json())
{'airdate': '2005-10-13',
 'download_url': 'https://static-uk2.ororo.tv/uploads/video/file/9/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage_1480525209.mp4?attachment=true&wmsAuthSign=aWQ9ODAzNDI3Kyt2aWRlbys5JnNlcnZlcl90aW1lPTIvOC8yMDE3IDI6Mjc6MDQgUE0maGFzaF92YWx1ZT1kbEpGM3c1bldSOXBOMUg5V2N1S0NnPT0mdmFsaWRtaW51dGVzPTk2MCZzdHJtX2xlbj05NQ%3D%3D',
 'id': 9,
 'name': 'Everybody Hates Sausage',
 'number': '4',
 'plot': 'When Julius buys a big crate of sausage, he makes everyone eat it '
         'with every meal. But Tonya refuses to, causing friction between the '
         'her and Rochelle. While at school, Chris is sentenced to 3 days of '
         'detention after a rumor goes round about him beating up the school '
         'bully, Joey.',
 'resolution': 'SD',
 'season': 1,
 'show_name': 'Everybody hates Chris',
 'subtitles': [{'lang': 'en',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/4867/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage.eng.vtt'},
               {'lang': 'ru',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/28629/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage.vtt'},
               {'lang': 'es',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/55744/1x04_EH_Sausage.vtt'},
               {'lang': 'pt',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/124429/Everybody_Hates_Chris_104_-_Everybody_Hates_Sausage.vtt'},
               {'lang': 'cs',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/217213/Everybody_Hates_Chris_104_-_Everybody_Hates_Sausages.vtt'},
               {'lang': 'tr',
                'url': 'https://uploads.ororo-mirror.tv/uploads/subtitle/file/192405/Everybody_Hates_Chris_S01E04_-_Everybody_Hates_Sausages-tur.vtt'}],
 'updated_at': 1480640069,
 'url': 'https://static-gra.ororo.tv/uploads/video/file/9/Everybody.Hates.Chris.S01E04.DVDRip.Everybody.Hates.Sausage_1480525209.smil/playlist.m3u8?wmsAuthSign=aWQ9ODAzNDI3Kyt2aWRlbys5JnNlcnZlcl90aW1lPTIvOC8yMDE3IDI6Mjc6MDQgUE0maGFzaF92YWx1ZT1FajlGK2JPMEd3aU1Lc3lnN1M4NlpBPT0mdmFsaWRtaW51dGVzPTk2MCZzdHJtX2xlbj05Ng%3D%3D'}
like image 172
Martijn Pieters Avatar answered Nov 02 '22 23:11

Martijn Pieters