Question: Fetching data from https url with Python 3.7
0
gravatar for konstanzeta
2.1 years ago by
konstanzeta0 wrote:

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 • 2.5k views
ADD COMMENTlink modified 2.1 years ago by Alex Reynolds31k • written 2.1 years ago by konstanzeta0
1
gravatar for Alex Reynolds
2.1 years ago by
Alex Reynolds31k
Seattle, WA USA
Alex Reynolds31k wrote:

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.

ADD COMMENTlink modified 2.1 years ago • written 2.1 years ago by Alex Reynolds31k

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.

ADD REPLYlink modified 2.1 years ago • written 2.1 years ago by konstanzeta0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2237 users visited in the last hour