1

I did

import requests

url = "https://randomuser.me/api/"
data = requests.get(url).json()


print data

I kept getting

Traceback (most recent call last):
  File "/Applications/MAMP/htdocs/code/python/curl.py", line 4, in <module>
    data = requests.get(url).json()
  File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL23_GET_SERVER_HELLO', 'sslv3 alert handshake failure')],)",)

BUT if I run this curl https://randomuser.me/api/

on my Terminal, I got

{"results":[{"gender":"male","name":{"title":"mr","first":"joscha","last":"maier"},"location":{"street":"7686 parkstraße","city":"kelheim","state":"hessen","postcode":46836},"email":"joscha.maier@example.com","login":{"username":"bluefrog937","password":"2003","salt":"xcZH4xOX","md5":"9fc3a4c2c8999066149d322ab4948669","sha1":"1c392831338c7b5715bd19a4c186111df32a6d04","sha256":"a21b5afa260d8d6a121d6f2dc688fb2f2b41e4f0b5c823daea76f968338653fb"},"dob":"1994-10-26 06:51:40","registered":"2009-05-25 10:37:41","phone":"0509-5347389","cell":"0174-6616884","id":{"name":"","value":null},"picture":{"large":"https://randomuser.me/api/portraits/men/26.jpg","medium":"https://randomuser.me/api/portraits/med/men/26.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/26.jpg"},"nat":"DE"}],"info":{"seed":"f74c00ae752dccac","results":1,"page":1,"version":"1.1"}}
code-8
  • 54,650
  • 106
  • 352
  • 604

1 Answers1

1

Your code works perfectly for me with Python 2.7.12. You are not giving the exact version of Python you are using but my guess is that it is a version before 2.7.9. In 2.7.9 several changes to the SSL implementation where done, including adding support for Server Name Indication (SNI). Your target URL needs support for SNI in the client as can be seen when testing the site with SSLLabs:

This site works only in browsers with SNI support.

The trace you show is typical for clients which don't support SNI. From the FAQ for requests:

Python3 and Python 2.7.9+ include native support for SNI in their SSL modules. For information on using SNI with Requests on Python < 2.7.9 refer to this Stack Overflow answer.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172