Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a https request in python 3

I want to download a file from my server using the https protocol. How should I go about doing this? This is my basic code with http

response=requests.get('http://url',stream='True')

handle=open('dest_file.txt','wb')
for chunk in response.iter_content(chunk_size=512):
    if chunk:  # filter out keep-alive new chunks
        handle.write(chunk)

handle.close()  

can the requests module be used for https as well?

like image 398
Siddharth Sampath Avatar asked Feb 06 '18 11:02

Siddharth Sampath


People also ask

How to make a HTTP request in Python?

Making a Request Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and a server.

What is http in Python programming?

If you need a refresher, then check out Socket Programming in Python (Guide). As protocols go, HTTP is one of the simpler ones. It was designed to send content over the Internet, like HTML, videos, images, and so on. This is done with an HTTP request and response.

What is a HTTP request used for?

A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and a server.

How do I create an HTTPS application in Python?

Chrome: Go to Settings > Advanced > Privacy and security > Manage certificates > Authorities. Firefox: Go to Settings > Preferences > Privacy & Security > View Certificates > Authorities. This covers the infrastructure required to create Python HTTPS applications in the real world. In the next section, you’ll apply these concepts to your own code.


1 Answers

According to http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

response=requests.get('https://url', stream='True', verify='your certificate.crt')

handle=open('dest_file.txt','wb')
for chunk in response.iter_content(chunk_size=512):
    if chunk:  # filter out keep-alive new chunks
        handle.write(chunk)

handle.close()  
like image 59
Wilhelm Liao Avatar answered Oct 26 '22 04:10

Wilhelm Liao