Python – HTTP Authentication

Python – HTTP Authentication ”; Previous Next 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. pip install requests Authenticating to Github Below we see a simple authentication mechanism involving only the username and the password. A successful response indicates valid login. import requests r = requests.get(”https://api.github.com/user”, auth=(”user”, ”pass”)) print r When we run the above program, we get the following output − 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. 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) 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. Print Page Previous Next Advertisements ”;

Python – HTTP Client

Python – HTTP Client ”; Previous Next In the http protocol, the request from the client reaches the server and fetches some data and metadata assuming it is a valid request. We can analyze this response from the server using various functions available in the python requests module. Here the below python programs run in the client side and display the result of the response sent by the server. Get Initial Response In the below program the get method from requests module fetches the data from a server and it is printed in plain text format. import requests r = requests.get(”https://httpbin.org/”) print(r.text)[:200] When we run the above program, we get the following output − <!DOCTYPE html > <html lang=”en”> <head> <meta charset=”UTF-8″> <title>httpbin.org</title> <link href=”https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+ Get Session Object Response The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance. If you’re making several requests to the same host, the underlying TCP connection will be reused. import requests s = requests.Session() s.get(”http://httpbin.org/cookies/set/sessioncookie/31251425”) r = s.get(”http://httpbin.org/cookies”) print(r.text) When we run the above program, we get the following output − {“cookies”:{“sessioncookie”:”31251425″}} Handling Error In case some error is raised because of issue in processing the request by the server, the python program can gracefully handle the exception raised using the timeout parameter as shown below. The program will wait for the defined value of the timeout error and then raise the time out error. requests.get(”http://github.com”, timeout=10.001) Print Page Previous Next Advertisements ”;

Python – Routing

Python – Routing ”; Previous Next Routing is the mechanism of mapping the URL directly to the code that creates the webpage. It helps in better management of the structure of the webpage and increases the performance of the site considerably and further enhancements or modifications become really straight forward. In python routing is implemented in most of the web frame works. We will see the examples from flask web framework in this chapter. Routing in Flask The route() decorator in Flask is used to bind an URL to a function. As a result when the URL is mentioned in the browser, the function is executed to give the result. Here, URL ”/hello” rule is bound to the hello_world() function. As a result, if a user visits http://localhost:5000/ URL, the output of the hello_world() function will be rendered in the browser. from flask import Flask app = Flask(__name__) @app.route(”/”) def hello_world(): return ”Hello Tutorialspoint” if __name__ == ”__main__”: app.run() When we run the above program, we get the following output − * Serving Flask app “flask_route” (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 – – [06/Aug/2018 08:48:45] “GET / HTTP/1.1” 200 – 127.0.0.1 – – [06/Aug/2018 08:48:46] “GET /favicon.ico HTTP/1.1” 404 – 127.0.0.1 – – [06/Aug/2018 08:48:46] “GET /favicon.ico HTTP/1.1″ 404 – We open the browser and point to the URL http://localhost:5000/ to see the result of the function being executed. Using URL Variables We can pass on URL variables using route to build URLS on the fly. For this we use the url_for() function which accepts name of the function as the first argument and the rest of the arguments as variable part of the URL rule. In the below example we pass the function names as arguments to the url_for function and print out the result when those lines are executed. from flask import Flask, url_for app = Flask(__name__) @app.route(”/”) def index(): pass @app.route(”/login”) def login(): pass @app.route(”/user/”) def profile(username): pass with app.test_request_context(): print url_for(”index”) print url_for(”index”, _external=True) print url_for(”login”) print url_for(”login”, next=”/”) print url_for(”profile”, username=”Tutorials Point”) When we run the above program, we get the following output − / http://localhost/ /login /login?next=%2F /user/Tutorials%20Point Redirects We can use the redirect function to redirect the user to another URL using routing. We mention the new URL as a return value of the function whihc should redirect the user. This is helpful when we temporarily divert the users to a different page when we are modifying an existing webpage. from flask import Flask, abort, redirect, url_for app = Flask(__name__) @app.route(”/”) def index(): return redirect(url_for(”login”)) @app.route(”/login”) def login(): abort(401) # this_is_never_executed() When the above code is executed, the base URL goes to login page which uses the abort function so that the code for login page is never executed. Print Page Previous Next Advertisements ”;

Python – POP3

Python – POP3 ”; Previous Next The pop3 protocol is an email protocol to download messages from the email-server. These messages can be stored in the local machine. Key Points POP is an application layer internet standard protocol. Since POP supports offline access to the messages, thus requires less internet usage time. POP does not allow search facility. In order to access the messaged, it is necessary to download them. It allows only one mailbox to be created on server. It is not suitable for accessing non mail data. POP commands are generally abbreviated into codes of three or four letters. Eg. STAT. POP Commands The following table describes some of the POP commands: S.N. Command Description 1 LOGINThis command opens the connection. 2 STATIt is used to display number of messages currently in the mailbox. 3 LISTIt is used to get the summary of messages where each message summary is shown. 4 RETRThis command helps to select a mailbox to access the messages. 5 DELEIt is used to delete a message. 6 RSETIt is used to reset the session to its initial state. 7 QUITIt is used to log off the session. Pyhton’s poplib module provides classes named pop() and pop3_SSL() which are used to achieve this requirement. We supply the hostname and port number as argument. In the below example we connect to a gmail server and retrieve the messages after supplying the login credentials. import poplib user = ”username” # Connect to the mail box Mailbox = poplib.POP3_SSL(”pop.googlemail.com”, ”995”) Mailbox.user(user) Mailbox.pass_(”password”) NumofMessages = len(Mailbox.list()[1]) for i in range(NumofMessages): for msg in Mailbox.retr(i+1)[1]: print msg Mailbox.quit() The messages are retrieved when the above program is run. Print Page Previous Next Advertisements ”;

Python – Request Status Codes

Python – Request Status Codes ”; Previous Next After receiving and interpreting a request message, a server responds with an HTTP response message. The response message has a Status-Code. It is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit: Status Codes S.N. Code and Description 1 1xx: Informational It means the request was received and the process is continuing. 2 2xx: Success It means the action was successfully received, understood, and accepted. 3 3xx: Redirection It means further action must be taken in order to complete the request. 4 4xx: Client Error It means the request contains incorrect syntax or cannot be fulfilled. 5 5xx: Server Error It means the server failed to fulfill an apparently valid request. Successful Response In the below example we access a file from a url and the response is successful. So the status code returned is 200. import urllib3 http = urllib3.PoolManager() resp = http.request(”GET”, ”http://tutorialspoint.com/robots.txt”) print resp.data # get the status of the response print resp.status When we run the above program, we get the following output − User-agent: * Disallow: /tmp Disallow: /logs Disallow: /rate/* Disallow: /cgi-bin/* Disallow: /videotutorials/video_course_view.php?* Disallow: /videotutorials/course_view.php?* Disallow: /videos/* Disallow: /*/*_question_bank/* Disallow: //*/*/*/*/src/* 200 Unsuccessful Response In the below example we access a file from a url which does not exist. The response is unsuccessful. So the status code returned is 403. import urllib3 http = urllib3.PoolManager() resp = http.request(”GET”, ”http://tutorialspoint.com/robot.txt”) print resp.data # get the status of the response print resp.status When we run the above program, we get the following output − <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don”t have permission to access /robot.txt on this server.</p> </body></html> 403 Print Page Previous Next Advertisements ”;

Python – HTTP Headers

Python – HTTP Headers ”; Previous Next The request and response between client and server involves header and body in the message. Headers contain protocol specific information that appear at the beginning of the raw message that is sent over TCP connection. The body of the message is separated from headers using a blank line. Example of Headers The headers in the http response can be categorized into following types. Below is a description of the header and an example. Cache-Control The Cache-Control general-header field is used to specify directives that MUST be obeyed by all the caching system. The syntax is as follows: Cache-Control : cache-request-directive|cache-response-directive An HTTP client or server can use the Cache-control general header to specify parameters for the cache or to request certain kinds of documents from the cache. The caching directives are specified in a comma-separated list. For example: Cache-control: no-cache Connection The Connection general-header field allows the sender to specify options that are desired for that particular connection and must not be communicated by proxies over further connections. Following is the simple syntax for using connection header: Connection : “Connection” HTTP/1.1 defines the “close” connection option for the sender to signal that the connection will be closed after completion of the response. For example: Connection: close By default, HTTP 1.1 uses persistent connections, where the connection does not automatically close after a transaction. HTTP 1.0, on the other hand, does not have persistent connections by default. If a 1.0 client wishes to use persistent connections, it uses the keep-alive parameter as follows: Connection: keep-alive Date All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. HTTP applications are allowed to use any of the following three representations of date/time stamps: Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 Sun Nov 6 08:49:37 1994 ; ANSI C”s asctime() format Transfer-Encoding The Transfer-Encoding general-header field indicates what type of transformation has been applied to the message body in order to safely transfer it between the sender and the recipient. This is not the same as content-encoding because transfer-encodings are a property of the message, not of the entity-body. The syntax of Transfer-Encoding header field is as follows: Transfer-Encoding: chunked All transfer-coding values are case-insensitive. Upgrade The Upgrade general-header allows the client to specify what additional communication protocols it supports and would like to use if the server finds it appropriate to switch protocols. For example: Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other, incompatible protocol. Via The Via general-header must be used by gateways and proxies to indicate the intermediate protocols and recipients. For example, a request message could be sent from an HTTP/1.0 user agent to an internal proxy code-named “fred”, which uses HTTP/1.1 to forward the request to a public proxy at nowhere.com, which completes the request by forwarding it to the origin server at www.ics.uci.edu. The request received by www.ics.uci.edu would then have the following Via header field: Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other, incompatible protocol. Warning The Warning general-header is used to carry additional information about the status or transformation of a message which might not be reflected in the message. A response may carry more than one Warning header. Warning : warn-code SP warn-agent SP warn-text SP warn-date Example In the below example we use the urllib2 module to get a response using urlopen. Next we apply the info() method to get the header information for that response. import urllib2 response = urllib2.urlopen(”http://www.tutorialspoint.com/python”) html = response.info() print html When we run the above program, we get the following output − Access-Control-Allow-Headers: X-Requested-With Access-Control-Allow-Origin: * Cache-Control: max-age=2592000 Content-Type: text/html; charset=UTF-8 Date: Mon, 02 Jul 2018 11:06:07 GMT Expires: Wed, 01 Aug 2018 11:06:07 GMT Last-Modified: Sun, 01 Jul 2018 21:05:38 GMT Server: ECS (tir/CDD1) Vary: Accept-Encoding X-Cache: HIT Content-Length: 22063 Connection: close Print Page Previous Next Advertisements ”;

Python – Directory Listing

Python – Directory Listing ”; Previous Next Python can be used to get the list of content from a directory. We can make program to list the content of directory which is in the same machine where python is running. We can also login to the remote system and list the content from the remote directory. Listing Local Directory In the below example we use the listdir() method to get the content of the current directory. To also indicate the type of the content like file or directory, we use more functions to evaluate the nature of the content. for name in os.listdir(”.”): if os.path.isfile(name): print ”file: ”, name elif os.path.isdir(name): print ”dir: ”, name elif os.path.islink(name): print ”link: ”, name else: print ”unknown”, name When we run the above program, we get the following output − file: abcl.htm dir: allbooks link: ulink Please note the content above is specific to the system where the python program was run. The result will vary depending on the system and its content. Listing Remote Directory We can list the content of the remote directory by using ftp to access the remote system. Once the connection is established we can use commands that will list the directory contents in a way similar to the listing of local directories. from ftplib import FTP def main(): ftp = FTP(”ftp.ibiblio.org”) ftp.login() ftp.cwd(”pub/academic/biology/”) # change to some other subject entries = ftp.nlst() ftp.quit() print(len(entries), “entries:”) for entry in sorted(entries): print(entry) if __name__ == ”__main__”: main() When we run the above program, we get the following output − (6, ”entries:”) INDEX README acedb dna-mutations ecology+evolution molbio Print Page Previous Next Advertisements ”;

Python – Uploading Data

Python – Uploading Data ”; Previous Next We can upload data to a serer using python”s module which handle ftp or File Transfer Protocol. We need to install the module ftplib to acheive this. pip install ftplib Using ftplib In the below example we use FTP method to connect to the server and then supply the user credentials. Next we mention the name of the file and the storbinary method to send and store the file in the server. import ftplib ftp = ftplib.FTP(“127.0.0.1”) ftp.login(“username”, “password”) file = open(”index.html”,”rb”) ftp.storbinary(“STOR ” + file, open(file, “rb”)) file.close() ftp.quit() When we run the above program, we observer that a copy of the file has been created in the server. Using ftpreety Similar to ftplib we can use ftpreety to connect securely to a remote server and upload file. We can aslo download file using ftpreety. The below program illustraits the same. from ftpretty import ftpretty # Mention the host host = “127.0.0.1” # Supply the credentisals f = ftpretty(host, user, pass ) # Get a file, save it locally f.get(”someremote/file/on/server.txt”, ”/tmp/localcopy/server.txt”) # Put a local file to a remote location # non-existent subdirectories will be created automatically f.put(”/tmp/localcopy/data.txt”, ”someremote/file/on/server.txt”) When we run the above program, we observer that a copy of the file has been created in the server. Print Page Previous Next Advertisements ”;

Python – Google Maps

Python – Google Maps ”; Previous Next Python provides modules which can be used to translate addresses available in google map directly to geographic coordinates. It is helpful in finding business addresses and locating the closeness of different addresses. We use a module named pygeocoder which provides the functionalities to receive addresses and geocodes. This module is installed through pip using the following command. Installing pygeocoder pip install pygeocoder Finding Business Address We submit a business name as input and the program gives the complete address as the output. The module uses data from google maps in the background to retrieve the result. from pygeocoder import Geocoder business_name = “Workafella Business Centre – Hitec city” print “Searching %s” %business_name results = Geocoder.geocode(business_name) for result in results: print result When we run the above program, we get the following output − Searching Workafella Business Centre – Hitec city Western pearl building 1st floor, Hitech City Rd, Opposite HDFC Bank, Kondapur, Hyderabad, Telangana 500084, India Print Page Previous Next Advertisements ”;

Python – Connection Re-use

Python – Connection Re-use ”; Previous Next When a client makes a valid request to a server, a temporary connection is established between them to complete the sending and receiving process. But there are scenarios where the connection needs to be kept alive as there is need of automatic requests and responses between the programs which are communicating. Take for example an interactive webpage. After the webpage is loaded there is a need of submitting a form data or downloading further CSS and JavaScript components. The connection needs to be kept alive for faster performance and an unbroken communication between the client and the server. Python provides urllib3 module which had methods to take care of connection reuse between a client and a server. In the below example we create a connection and make multiple requests by passing different parameters with the GET request. We receive multiple responses but we also count the number of connection that has been used in the process. As we see the number of connection does not change implying the reuse of the connection. from urllib3 import HTTPConnectionPool pool = HTTPConnectionPool(”ajax.googleapis.com”, maxsize=1) r = pool.request(”GET”, ”/ajax/services/search/web”, fields={”q”: ”python”, ”v”: ”1.0”}) print ”Response Status:”, r.status # Header of the response print ”Header: ”,r.headers[”content-type”] # Content of the response print ”Python: ”,len(r.data) r = pool.request(”GET”, ”/ajax/services/search/web”, fields={”q”: ”php”, ”v”: ”1.0”}) # Content of the response print ”php: ”,len(r.data) print ”Number of Connections: ”,pool.num_connections print ”Number of requests: ”,pool.num_requests When we run the above program, we get the following output − Response Status: 200 Header: text/javascript; charset=utf-8 Python: 211 php: 211 Number of Connections: 1 Number of requests: 2 Print Page Previous Next Advertisements ”;