This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Fetching data from https url with Python 3.7

Hi all,

How do I use urllib.request module in Python 3.7 to open a pdb file?

The URL I’m using is http://files.rcsb.org/download/1N5O.pdb.

I’ve read about the need for SSL support but I’m not too sure how to write a code which incorporates those principles.

Here is how my code looks like thus far:

import urllib.request req= urllib.request.Request("http://files.rcsb.org/download/1N5O.pdb")

with urllib.request.urlopen(req) as response:

          the_page= response.read()

print (the_page)

Any advice on the above would be much appreciated. Thank you!

(For more accurate formatting, I've attached my picture here in [1]:

https pdb

1 answer

You wouldn't need SSL or https support to do an http GET request over an unencrypted connection.

That said, you could do something like:

#!/usr/bin/env python

import sys
import requests

url = 'http://files.rcsb.org/download/1N5O.pdb'
localFn = '1N5O.pdb'

try:
    r = requests.get(url, stream=True)
    with open(localFn, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024*1024):
            if chunk:
                f.write(chunk)
except requests.exceptions.ChunkedEncodingError as cee:
    sys.stderr.write('Error: Could not get data')
    sys.exit(-1)

See: http://docs.python-requests.org/en/master/user/advanced/ for a discussion of SSL/https GET requests.

NB: This will overwrite any existing file with the name 1N5O.pdb. Use os.path.exists if you want to do something a little more robust.

Hi Alex,

Thank you very much for your help.

I have applied these to my code, and its working fine now.

Have a good week ahead.

Log in to answer this question.