- Python Network Programming - Home
- Python Network Introduction
- Python Networking - Environment Setup
- Python Networking - Internet Protocol
- Python Networking - IP Address
- Python Networking - DNS Lookup
- Python Networking - Routing
- Python Networking - HTTP Requests
- Python Networking - HTTP Response
- Python Networking - HTTP Headers
- Python Networking - Custom HTTP Requests
- Python Networking - Request Status Codes
- Python Networking - HTTP Authentication
- Python Networking - HTTP Data Download
- Python Networking - Connection Re-use
- Python Networking - Network Interface
- Python Networking - Sockets Programming
- Python Networking - HTTP Client
- Python Networking - HTTP Server
- Python Networking - Building URLs
- Python Networking - WebForm Submission
- Python Networking - Databases and SQL
- Python Networking - Telnet
- Python Networking - Email Messages
- Python Networking - SMTP
- Python Networking - POP3
- Python Networking - IMAP
- Python Networking - SSH
- Python Networking - FTP
- Python Networking - SFTP
- Python Networking - Web Servers
- Python Networking - Uploading Data
- Python Networking - Proxy Server
- Python Networking - Directory Listing
- Python Networking - Remote Procedure Call
- Python Networking - RPC JSON Server
- Python Networking - Google Maps
- Python Networking - RSS Feed
Python Network Programming Resources
Python Network - Http Authentication
Authentication is the process of determining if the request has come from a valid user who has the required privileges to use the system. In the world of computer networking this is a very vital requirement as many systems keep interacting with each other and proper mechanism needs to ensure that only valid interactions happen between these programs.
The python module names requests has in-built feature to call various APIs provided by the serving web apps along with the user credentials. These credentials have to be embedded in the calling program. If the APIs verify it successfully then a valid login happens.
Installing Requests
We install the required python module named requests for running the authentication program.
pip3 install requests requests_oauthlib
Authenticating to Github
Below we see a simple authentication mechanism involving only the username and the password. A successful response indicates valid login.
main.py
import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print(r)
Output
When we run the above program, we get the following output −
<Response [401]>
Authenticating to Twitter
We can also run a program to use twitter's api and make a successful login by using the following code. We use the OAuth1 method available in the requests module to process the parameters required by Twitter API. As we can see the requests module is capable of handling more complex authentication mechanism involving keys and tokens rather than just the username and password mechanism.
main.py
import requests
from requests_oauthlib import OAuth1
url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',
'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')
requests.get(url, auth=auth)
Output
When we run the above program, we get the following output −
{
"errors": [
{
"code": 215,
"message": "Bad Authentication data."
}
]
}
But using the proper values for OAuth1 parameters you get a successful response.