Send HTTP with Basic authentication header in Python

A note that sends HTTP with a Basic authentication header in Python.

I use urllib2, but according to the manual, HTTPPasswordMgrWithDefaultRealm and HTTPBasicAuthHandler are deadly annoying.

In such a case, you can write the header yourself.

It's easy to talk about, just put the value Basic (base64 hash value of" username: password ") in the header key ʻauthorization`.

When written in code

headers["authorization"] = "Basic " + (user + ":" + pass).encode("base64")[:-1]

OK. The last [: -1] has the line feed code removed.

The whole code looks like the following

import urllib2
url = "http://sample.com/index.html"
user = "XXXX"
password = "XXXX"
headers ={}
headers["authorization"] = "Basic " + (user + ":" + password).encode("base64")[:-1]
req = urllib2.Request(url=url, headers=headers)
res = urllib2.urlopen(req)
print(res.read())

It was really easy.

Recommended Posts

Send HTTP with Basic authentication header in Python
BASIC authentication with Python bottle
Addictive point when going through http proxy with basic authentication in python
Scraping with Selenium in Python (Basic)
Basic authentication with an encrypted password (.htpasswd) in bottle with python
Basic sorting in Python
Http request in python
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
HTTP communication with Python
Send email with Python
Send an email with Excel attached in Python
Send Gmail in Python
Send Japanese email with Python3
Scraping with selenium in Python
Refactoring Learned in Python (Basic)
Scraping with chromedriver in python
Debugging with pdb in Python
Use HTTP cache in Python
[Python] Using OpenCV with Python (Basic)
Working with sounds in Python
Scraping with Selenium in Python
Send email in Python (Outlook)
Scraping with Tor in Python
Tweet with image in Python
Easy HTTP server with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Send using Python with Gmail
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
CSS parsing with cssutils in Python
Write an HTTP / 2 server in Python
[Python] Use Basic/Digest authentication with Flask
Numer0n with items made in Python
Open UTF-8 with BOM in Python
[Python] Basic knowledge used in AtCoder
Use rospy with virtualenv in Python3
Basic authentication and Digest authentication with Flask
Use Python in pyenv with NeoVim
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
[Python] Send an email with outlook
Send multipart / form-data with python requests
Get started with Python in Blender
Send image with python, save with php
Working with DICOM images in Python
Send email via gmail with Python 3.4.3.
Basic study of OpenCV with Python
Firebase Authentication token issuance in Python and token verification with Fast API
Basic Linear Algebra Learned in Python (Part 1)
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Getting Started with python3 # 1 Learn Basic Knowledge
Python3> in keyword> True with partial match?
A simple HTTP client implemented in Python