Python – Network Interface

Python – Network Interface ”; Previous Next When we have multiple interfaces in a machine we need to keep track of their names, status etc. In Python we can list the interfaces and their status. Example In the below example we use the python module netifaces which gives the details of the interfaces and their status. The methods used are very simple and straight forward. import netifaces print (netifaces.interfaces()) print (netifaces.ifaddresses(”lo”)) print (netifaces.AF_LINK) addrs = netifaces.ifaddresses(”ens33”) print(addrs[netifaces.AF_INET]) print(addrs[netifaces.AF_LINK]) When we run the above program, we get the following output − # Result [”lo”, ”ens33”] {17: [{”peer”: ”00:00:00:00:00:00”, ”addr”: ”00:00:00:00:00:00”}], 2: [{”peer”: ”127.0.0.1”, ”addr”: ”127.0.0.1”, ”netmask”: ”255.0.0.0”}], 10: [{”addr”: ”::1”, ”netmask”: ”ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128”}]} 17 [{”netmask”: ”255.255.255.0”, ”addr”: ”192.168.232.128”, ”broadcast”: ”192.168.232.255”}] [{”addr”: ”00:0c:29:ea:13:0a”, ”broadcast”: ”ff:ff:ff:ff:ff:ff”}] Print Page Previous Next Advertisements ”;

Python – Sockets Programming

Python – Sockets Programming ”; Previous Next Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols. Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. We use the socket module in python to create and use sockets. Sockets have their own vocabulary − Sr.No. Term & Description 1 Domain The family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on. 2 type The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols. 3 protocol Typically zero, this may be used to identify a variant of a protocol within a domain and type. 4 hostname The identifier of a network interface − A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation A string “<broadcast>”, which specifies an INADDR_BROADCAST address. A zero-length string, which specifies INADDR_ANY, or An Integer, interpreted as a binary address in host byte order. 5 port Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a string containing a port number, or the name of a service. The socket Module To create a socket, you must use the socket.socket() function available in socket module, which has the general syntax − s = socket.socket (socket_family, socket_type, protocol=0) Here is the description of the parameters − socket_family − This is either AF_UNIX or AF_INET, as explained earlier. socket_type − This is either SOCK_STREAM or SOCK_DGRAM. protocol − This is usually left out, defaulting to 0. Once you have socket object, then you can use required functions to create your client or server program. Server Socket Methods Sr.No. Method & Description 1 s.bind() This method binds address (hostname, port number pair) to socket. 2 s.listen() This method sets up and start TCP listener. 3 s.accept() This passively accept TCP client connection, waiting until connection arrives (blocking). Client Socket Methods Sr.No. Method & Description 1 s.connect() This method actively initiates TCP server connection. General Socket Methods Sr.No. Method & Description 1 s.recv() This method receives TCP message 2 s.send() This method transmits TCP message 3 s.recvfrom() This method receives UDP message 4 s.sendto() This method transmits UDP message 5 s.close() This method closes socket 6 socket.gethostname() Returns the hostname. A Simple Server To write Internet servers, we use the socket function available in socket module to create a socket object. A socket object is then used to call other functions to setup a socket server. Now call bind(hostname, port) function to specify a port for your service on the given host. Next, call the accept method of the returned object. This method waits until a client connects to the port you specified, and then returns a connection object that represents the connection to that client. #!/usr/bin/python # This is server.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print ”Got connection from”, addr c.send(”Thank you for connecting”) c.close() # Close the connection A Simple Client Let us write a very simple client program which opens a connection to a given port 12345 and given host. This is very simple to create a socket client using Python”s socket module function. The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file. The following code is a very simple client that connects to a given host and port, reads any available data from the socket, and then exits − #!/usr/bin/python # This is client.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done Now run this server.py in background and then run above client.py to see the result. # Following would start a server in background. $ python server.py & # Once server is started run client as follows: $ python client.py This would produce following result − Got connection from (”127.0.0.1”, 48437) Thank you for connecting Socket with Public URL In the below example we use few methods from the socket module to find the address information of server and host name details. import socket from pprint import pprint # get server address addrinfo = socket.getaddrinfo(”tutorialspoint.com”, ”www”) pprint(addrinfo) # get server hostname print socket.gethostname() When we run the above program, we get the following output − [(2, 1, 0, ””, (”94.130.81.180”, 80))] DESKTOP-JXYKQCPLP Print Page Previous Next Advertisements ”;

Python – WebForm Submission

Python – WebForm Submission ”; Previous Next Often the interaction with a webpage needs some data to be submitted to the server through the forms present in the html page. These webforms are typically used for processes like signing up for a new account or supplying some information like name or roll number to retrieve the result of an examination. The requests module handles this gracefully using the POST method with the required parameters. Example In the below example we use the sign up form of a website by supplying the userid and password value. After the submission of the values we print the response. import requests ID_USERNAME = ”signup-user-name” ID_PASSWORD = ”signup-user-password” USERNAME = ”username” PASSWORD = ”yourpassword” SIGNUP_URL = ”http://codepad.org/login” def submit_form(): “””Submit a form””” payload = {ID_USERNAME : USERNAME, ID_PASSWORD : PASSWORD,} resp = requests.get(SIGNUP_URL) print “Response to GET request: %s” %resp.content resp = requests.post(SIGNUP_URL, payload) print “Headers from a POST request response: %s” %resp.headers #print “HTML Response: %s” %resp.read() if __name__ == ”__main__”: submit_form() When we run the above program, we get the following output − Response to GET request: <!DOCTYPE html> <html> <head> <meta http-equiv=”content-type” content=”text/html; charset=UTF-8″> <meta HTTP-EQUIV=”Pragma” CONTENT=”no-cache”> <meta HTTP-EQUIV=”Expires” CONTENT=”-1″> <title>Login – codepad</title> <link href=”/main.css” media=”screen” rel=”stylesheet” type=”text/css” /> <style type=”text/css”> </style> <script src=”https://www.google.com/recaptcha/api.js”></script> <script> function onRecaptcha(token) { document.getElementById(“editor-form”).submit(); } </script> </head> <body > ………………… ………………… Print Page Previous Next Advertisements ”;

Python – Databases and SQL

Python – Databases and SQL ”; Previous Next The Python programming language has powerful features for database programming. Python supports various databases like SQLite, MySQL, Oracle, Sybase, PostgreSQL, etc. Python also supports Data Definition Language (DDL), Data Manipulation Language (DML) and Data Query Statements. The Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard. Here is the list of available Python database interfaces: Python Database Interfaces and APIs. You must download a separate DB API module for each database you need to access. In this chapter we will see the use of SQLite database in python programming language. It is done by using python’s inbuilt, sqlite3 module. You should first create a connection object that represents the database and then create some cursor objects to execute SQL statements. Connect To Database Following Python code shows how to connect to an existing database. If the database does not exist, then it will be created and finally a database object will be returned. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; Here, you can also supply database name as the special name :memory: to create a database in RAM. Now, let”s run the above program to create our database test.db in the current directory. You can change your path as per your requirement. Keep the above code in sqlite.py file and execute it as shown below. If the database is successfully created, then it will display the following message. $chmod +x sqlite.py $./sqlite.py Open database successfully Create a Table Following Python program will be used to create a table in the previously created database. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; conn.execute(”””CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);”””) print “Table created successfully”; conn.close() When the above program is executed, it will create the COMPANY table in your test.db and it will display the following messages − Opened database successfully Table created successfully Insert Operation Following Python program shows how to create records in the COMPANY table created in the above example. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; conn.execute(“INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, ”Paul”, 32, ”California”, 20000.00 )”); conn.execute(“INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, ”Allen”, 25, ”Texas”, 15000.00 )”); conn.execute(“INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, ”Teddy”, 23, ”Norway”, 20000.00 )”); conn.execute(“INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, ”Mark”, 25, ”Rich-Mond ”, 65000.00 )”); conn.commit() print “Records created successfully”; conn.close() When the above program is executed, it will create the given records in the COMPANY table and it will display the following two lines − Opened database successfully Records created successfully Select Operation Following Python program shows how to fetch and display records from the COMPANY table created in the above example. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; cursor = conn.execute(“SELECT id, name, address, salary from COMPANY”) for row in cursor: print “ID = “, row[0] print “NAME = “, row[1] print “ADDRESS = “, row[2] print “SALARY = “, row[3], “n” print “Operation done successfully”; conn.close() When the above program is executed, it will produce the following result. Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000.0 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully Update Operation Following Python code shows how to use UPDATE statement to update any record and then fetch and display the updated records from the COMPANY table. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; conn.execute(“UPDATE COMPANY set SALARY = 25000.00 where ID = 1”) conn.commit print “Total number of rows updated :”, conn.total_changes cursor = conn.execute(“SELECT id, name, address, salary from COMPANY”) for row in cursor: print “ID = “, row[0] print “NAME = “, row[1] print “ADDRESS = “, row[2] print “SALARY = “, row[3], “n” print “Operation done successfully”; conn.close() When the above program is executed, it will produce the following result. Opened database successfully Total number of rows updated : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 25000.0 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully Delete Operation Following Python code shows how to use DELETE statement to delete any record and then fetch and display the remaining records from the COMPANY table. #!/usr/bin/python import sqlite3 conn = sqlite3.connect(”test.db”) print “Opened database successfully”; conn.execute(“DELETE from COMPANY where ID = 2;”) conn.commit() print “Total number of rows deleted :”, conn.total_changes cursor = conn.execute(“SELECT id, name, address, salary from COMPANY”) for row in cursor: print “ID = “, row[0] print “NAME = “, row[1] print “ADDRESS = “, row[2] print “SALARY = “, row[3], “n” print “Operation done successfully”; conn.close() When the above program is executed, it will produce the following result. Opened database successfully Total number of rows deleted : 1 ID = 1 NAME = Paul ADDRESS = California SALARY = 20000.0 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000.0 Operation done successfully Print Page Previous Next Advertisements ”;

Python – Telnet

Python – Telnet ”; Previous Next Telnet is a type of network protocol which allows a user in one computer to logon to another computer which also belongs to the same network. The telnet command is used along with the host name and then the user credentials are entered. Upon successful login the remote user can access the applications and data in a way similar to the regular user of the system. Of course some privileges can be controlled by the administrator of the system who sets up and maintains the system. In Python telnet is implemented by the module telnetlib which has the Telnet class which has the required methods to establish the connection. In the below example we also use the getpass module to handle the password prompt as part of the login process. Also we assume the connection is made to a unix host. The various methods from telnetlib.Telnet class used in the program are explained below. Telnet.read_until – Read until a given string, expected, is encountered or until timeout seconds have passed. Telnet.write – Write a string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise socket.error if the connection is closed. Telnet.read_all() – Read all data until EOF; block until connection closed. Example import getpass import telnetlib HOST = “http://localhost:8000/” user = raw_input(“Enter your remote account: “) password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until(“login: “) tn.write(user + “n”) if password: tn.read_until(“Password: “) tn.write(password + “n”) tn.write(“lsn”) tn.write(“exitn”) print tn.read_all() When we run the above program, we get the following output − – lrwxrwxrwx 1 0 0 1 Nov 13 2012 ftp -> . – lrwxrwxrwx 1 0 0 3 Nov 13 2012 mirror -> pub – drwxr-xr-x 23 0 0 4096 Nov 27 2017 pub – drwxr-sr-x 88 0 450 4096 May 04 19:30 site – drwxr-xr-x 9 0 0 4096 Jan 23 2014 vol Please note that this output is specific to the remote computer whose details are submitted when the program is run. Print Page Previous Next Advertisements ”;

Python – SFTP

Python – SFTP ”; Previous Next SFTP is also known as the SSH File Transfer Protocol. It is a network protocol that provides file access, file transfer, and file management over any reliable data stream. The program is run over a secure channel, such as SSH, that the server has already authenticated the client, and that the identity of the client user is available to the protocol. The pysftp module is a simple interface to SFTP. The module offers high level abstractions and task based routines to handle the SFTP needs. So we install the module into our python environment with the below command. pip install pysftp Example In the below example we login to a remote server using sftp and then get and put some file in that directory. import pysftp with pysftp.Connection(”hostname”, username=”me”, password=”secret”) as sftp: with sftp.cd(”/allcode”): # temporarily chdir to allcode sftp.put(”/pycode/filename”) # upload file to allcode/pycode on remote sftp.get(”remote_file”) # get a remote file When we run the above code we are able to see the list of files present in the allcode directory and also put and get some file in that directory. Print Page Previous Next Advertisements ”;

Python – HTTP Data Download

Python – HTTP Data Download ”; Previous Next We can download data from a serer using python”s module which handle ftp or File Transfer Protocol. We can also read the data and later save it to the local system. We need to install the module ftplib to acheive this. pip install ftplib Fetching the Files We can fetch a specific file by using the getfile method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated. import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary(“RETR ” + filename ,open(filename, ”wb”).write) except: print “Error” ftp = ftplib.FTP(“ftp.nluug.nl”) ftp.login(“anonymous”, “ftplib-example-1″) ftp.cwd(”/pub/”) change directory to /pub/ getFile(ftp,”README.nluug”) ftp.quit() When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated. Reading the Data In the below example we use the module urllib2 to read the required portion of the data which we can copy and save it to local system. When we run the above program, we get the following output − import urllib2 response = urllib2.urlopen(”http://www.tutorialspoint.com/python”) html = response.read(200) print html When we run the above program, we get the following output − <!DOCTYPE html> <!–[if IE 8]><html class=”ie ie8″> <![endif]–> <!–[if IE 9]><html class=”ie ie9″> <![endif]–> <!–[if gt IE 9]><!–> <html> <!–<![endif]–> <head> <!– Basic –> <meta charset=”ut Print Page Previous Next Advertisements ”;

Python – HTTP Response

Python – HTTP Response ”; Previous Next The http or Hyper Text Transfer Protocol works on client server model. Usually the web browser is the client and the computer hosting the website is the server. Upon receiving a request from client the server generates a response and sends it back to the client in certain format. After receiving and interpreting a request message, a server responds with an HTTP response message: A Status-line Zero or more header (General|Response|Entity) fields followed by CRLF An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields Optionally a message-body The following sections explain each of the entities used in an HTTP response message. Message Status-Line A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters. Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF HTTP Version A server supporting HTTP version 1.1 will return the following version information: HTTP-Version = HTTP/1.1 Status Code The Status-Code element 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: 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. HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes. Using Python Requests In the below python program we use the urllib3 module to make a http GET request and receive the response containing the data. It also provides the response code which is also managed by the functions in the module. The PoolManager object handles all of the details of connection pooling and also handles the thread safety. 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 Print Page Previous Next Advertisements ”;

Python – Building URLs

Python – Building URLs ”; Previous Next The requests module can help us build the URLS and manipulate the URL value dynamically. Any sub-directory of the URL can be fetched programmatically and then some part of it can be substituted with new values to build new URLs. Build_URL The below example uses urljoin to fetch the different subfolders in the URL path. The urljoin method is used to add new values to the base URL. from requests.compat import urljoin base=”https://stackoverflow.com/questions/3764291” print urljoin(base,”.”) print urljoin(base,”..”) print urljoin(base,”…”) print urljoin(base,”/3764299/”) url_query = urljoin(base,”?vers=1.0”) print url_query url_sec = urljoin(url_query,”#section-5.4”) print url_sec When we run the above program, we get the following output − https://stackoverflow.com/questions/ https://stackoverflow.com/ https://stackoverflow.com/questions/… https://stackoverflow.com/3764299/ https://stackoverflow.com/questions/3764291?vers=1.0 https://stackoverflow.com/questions/3764291?vers=1.0#section-5.4 Split the URLS The URLs can also be split into many parts beyond the main address. The additional parameters which are used for a specific query or tags attached to the URL are separated by using the urlparse method as shown below. from requests.compat import urlparse url1 = ”https://docs.python.org/2/py-modindex.html#cap-f” url2=”https://docs.python.org/2/search.html?q=urlparse” print urlparse(url1) print urlparse(url2) When we run the above program, we get the following output − ParseResult(scheme=”https”, netloc=”docs.python.org”, path=”/2/py-modindex.html”, params=””, query=””, fragment=”cap-f”) ParseResult(scheme=”https”, netloc=”docs.python.org”, path=”/2/search.html”, params=””, query=”q=urlparse”, fragment=””) Print Page Previous Next Advertisements ”;

Python – Email Messages

Python – Email Messages ”; Previous Next Email is a service which allows us to send the message in electronic mode over the internet. It offers an efficient, inexpensive and real time mean of distributing information among people. E-Mail Address Each user of email is assigned a unique name for his email account. This name is known as E-mail address. Different users can send and receive messages according to the e-mail address. E-mail is generally of the form username@domainname. For example, [email protected] is an e-mail address where webmaster is username and tutorialspoint.com is domain name. The username and the domain name are separated by @ (at) symbol. E-mail addresses are not case sensitive. Spaces are not allowed in e-mail address. The first five lines of an E-mail message is called E-mail header. The header part comprises of following fields: From Date To Subject CC BCC From The From field indicates the sender’s address i.e. who sent the e-mail. Date The Date field indicates the date when the e-mail was sent. To The To field indicates the recipient’s address i.e. to whom the e-mail is sent. Subject The Subject field indicates the purpose of e-mail. It should be precise and to the point. CC CC stands for Carbon copy. It includes those recipient addresses whom we want to keep informed but not exactly the intended recipient. BCC BCC stands for Black Carbon Copy. It is used when we do not want one or more of the recipients to know that someone else was copied on the message. Greeting Greeting is the opening of the actual message. Eg. Hi Sir or Hi Guys etc. Text It represents the actual content of the message. Signature This is the final part of an e-mail message. It includes Name of Sender, Address, and Contact Number. Python has EmailMessage class which can be used build email messages. This class ahs the required methods to customize different parts of the email message like – the TO and FROM tags, the Subject Line as well as the content of the email. Example In the below example we create an email message with all the necessary parts of an email. Once we print out the content of the message we can see the complete email. import email.message, email.policy, email.utils, sys text = “””Welcome to TutorialsPoint – Simple Easy Learning””” message = email.message.EmailMessage(email.policy.SMTP) message[”To”] = ”[email protected]” message[”From”] = ”Learn ” message[”Subject”] = ”A mail To you” message[”Date”] = email.utils.formatdate(localtime=True) message[”Message-ID”] = email.utils.make_msgid() message.set_content(text) sys.stdout.buffer.write(message.as_bytes()) When we run the above program, we get the following output − To: [email protected] From: Learn Subject: A mail To you Date: Wed, 13 Jun 2018 06:51:09 -0700 Message-ID: Content-Type: text/plain; charset=”utf-8″ Content-Transfer-Encoding: 7bit MIME-Version: 1.0 Welcome to TutorialsPoint – Simple Easy Learning Print Page Previous Next Advertisements ”;