Python – RPC JSON Server ”; Previous Next JSON or JavaScript Object Notation is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. The RPC call made based on JSON is able to send data in a much compact and efficient manner than the normal XML based RPC call. The python module jsonrpclib is able to create a simple JSON based server and client. Example In the below example we create a simple JSON server and create a function in it. This function breaks a bigger list into smaller lists mentioning the length of the argument as well as the argument itself. # server program from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer def findlen(*args): res = [] for arg in args: try: lenval = len(arg) except TypeError: lenval = None res.append((lenval, arg)) return res def main(): server = SimpleJSONRPCServer((”localhost”, 1006)) server.register_function(findlen) print(“Start server”) server.serve_forever() if __name__ == ”__main__”: main() # Call by client from jsonrpclib import Server def main(): conn = Server(”http://localhost:1006”) print(conn.findlen((”a”,”x”,”d”,”z”), 11, {”Mt. Abu”: 1602, ”Mt. Nanda”: 3001,”Mt. Kirubu”: 102, ”Mt.Nish”: 5710})) if __name__ == ”__main__”: main() When we run the above program, we get the following output − [[4, [u”a”, u”x”, u”d”, u”z”]], [None, 11], [4, {u”Mt. Abu”: 1602, u”Mt. Kirubu”: 102, u”Mt. Nanda”: 3001, u”Mt.Nish”: 5710}]] Print Page Previous Next Advertisements ”;
Category: python Network Programming
Python – Web Servers
Python – Web Servers ”; Previous Next Python is versatile enough to create many types of applications ans programs that drive the internet or other computer networks. One important aspect of internet is the web servers that are at the root of the client server model. In this chapter we will see few web servers which are created uaing pure python language. Gunicorn Gunicorn is a stand-alone web server which has a central master process tasked with managing the initiated worker processes of differing types. These worker processes then handle and deal with the requests directly. And all this can be configured and adapted to suit the diverse needs of production scenarios. Important Features It supports WSGI and can be used with any WSGI running Python application and framework It can also be used as a drop-in replacement for Paster (ex: Pyramid), Django”s Development Server, web2py, etc Offers the choice of various worker types/configurations and automatic worker process management HTTP/1.0 and HTTP/1.1 (Keep-Alive) support through synchronous and asynchronous workers Comes with SSL support Extensible with hooks CherryPy WSGI Server CherryPy is a self contained web framework as it can run on its own without the need of additional software. It has its own WSGI, HTTP/1.1-compliant web server. As it is a WSGI server, it can be used to serve any other WSGI Python application as well, without being bound to CherryPy”s application development framework. Important Features It can run any Python web applications running on WSGI. It can handle static files and it can just be used to serve files and folders alone. It is thread-pooled. It comes with support for SSL. It is an easy to adapt, easy to use pure-Python alternative which is robust and reliable. Twisted Web It is a web server that comes with the Twisted networking library. Whereas Twisted itself is “an event-driven networking engine”, the Twisted Web server runs on WSGI and it is capable of powering other Python web applications. Important Features It runs WSGI Python applications It can act like a Python web server framework, allowing you to program it with the language for custom HTTP serving purposes It offers simple and fast prototyping ability through Python Scrips (.rpy) which are executed upon HTTP requests It comes with proxy and reverse-proxy capabilities It supports Virtual Hosts • It can even serve Perl, PHP et cetera Print Page Previous Next Advertisements ”;
Python – IMAP
Python – IMAP ”; Previous Next IMAP is an email retrieval protocol which does not download the emails. It just reads them and displays them. This is very useful in low bandwidth condition. Python’s client side library called imaplib is used for accessing emails over imap protocol. IMAP stands for Internet Mail Access Protocol. It was first proposed in 1986. Key Points: IMAP allows the client program to manipulate the e-mail message on the server without downloading them on the local computer. The e-mail is hold and maintained by the remote server. It enables us to take any action such as downloading, delete the mail without reading the mail.It enables us to create, manipulate and delete remote message folders called mail boxes. IMAP enables the users to search the e-mails. It allows concurrent access to multiple mailboxes on multiple mail servers. IMAP Commands The following table describes some of the IMAP commands: S.N. Command Description 1 IMAP_LOGINThis command opens the connection. 2 CAPABILITYThis command requests for listing the capabilities that the server supports. 3 NOOPThis command is used as a periodic poll for new messages or message status updates during a period of inactivity. 4 SELECTThis command helps to select a mailbox to access the messages. 5 EXAMINEIt is same as SELECT command except no change to the mailbox is permitted. 6 CREATEIt is used to create mailbox with a specified name. 7 DELETEIt is used to permanently delete a mailbox with a given name. 8 RENAMEIt is used to change the name of a mailbox. 9 LOGOUTThis command informs the server that client is done with the session. The server must send BYE untagged response before the OK response and then close the network connection. Example In the below example we login to a gmail server with user credentials. Then we choose to display the messages in the inbox. A for loop is used to display the fetched messages one by one and finally the connection is closed. import imaplib import pprint imap_host = ”imap.gmail.com” imap_user = ”[email protected]” imap_pass = ”password” # connect to host using SSL imap = imaplib.IMAP4_SSL(imap_host) ## login to server imap.login(imap_user, imap_pass) imap.select(”Inbox”) tmp, data = imap.search(None, ”ALL”) for num in data[0].split(): tmp, data = imap.fetch(num, ”(RFC822)”) print(”Message: {0}n”.format(num)) pprint.pprint(data[0][1]) break imap.close() Depending on the mail box configuration, mail is displayed. Print Page Previous Next Advertisements ”;
Python – SSH
Python – SSH ”; Previous Next SSH or Secure Socket Shell, is a network protocol that provides a secure way to access a remote computer. Secure Shell provides strong authentication and secure encrypted data communications between two computers connecting over an insecure network such as the Internet. SSH is widely used by network administrators for managing systems and applications remotely, allowing them to log in to another computer over a network, execute commands and move files from one computer to another. AS cloud servers become more affordable, SSH is the most commonly used tool to perform various tasks on cloud server. We need it for &;minus Setup a web server for a client”s website Deploy source code to a production server In python SSH is implemented by using the python library called fabric. It can be used to issue commands remotely over SSH. Example In the below example we connect to a host and issue the command to identify the host type. We capture the result in and display it as a formatted text. from fabric import Connection result = Connection(”xyz.com”).run(”uname -s”) msg = “Ran {.command!r} on {.connection.host}, got stdout:n{.stdout}” print(msg.format(result)) When we run the above program, we get the following output − Linux This is a sample result which will depend on the server. Print Page Previous Next Advertisements ”;
Python – RSS Feed
Python – RSS Feed ”; Previous Next RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. In python we take help of the below package to read and process these feeds. pip install feedparser Feed Structure In the below example we get the structure of the feed so that we can analyze further about which parts of the feed we want to process. import feedparser NewsFeed = feedparser.parse(“https://timesofindia.indiatimes.com/rssfeedstopstories.cms”) entry = NewsFeed.entries[1] print entry.keys() When we run the above program, we get the following output − [”summary_detail”, ”published_parsed”, ”links”, ”title”, ”summary”, ”guidislink”, ”title_detail”, ”link”, ”published”, ”id”] Feed Title and Posts In the below example we read the title and head of the rss feed. import feedparser NewsFeed = feedparser.parse(“https://timesofindia.indiatimes.com/rssfeedstopstories.cms”) print ”Number of RSS posts :”, len(NewsFeed.entries) entry = NewsFeed.entries[1] print ”Post Title :”,entry.title When we run the above program we get the following output − Number of RSS posts : 5 Post Title : Cong-JD(S) in SC over choice of pro tem speaker Feed Details Based on above entry structure we can derive the necessary details from the feed using python program as shown below. As entry is a dictionary we utilize its keys to produce the values needed. import feedparser NewsFeed = feedparser.parse(“https://timesofindia.indiatimes.com/rssfeedstopstories.cms”) entry = NewsFeed.entries[1] print entry.published print “******” print entry.summary print “——News Link——–” print entry.link When we run the above program we get the following output − Fri, 18 May 2018 20:13:13 GMT ****** Controversy erupted on Friday over the appointment of BJP MLA K G Bopaiah as pro tem speaker for the assembly, with Congress and JD(S) claiming the move went against convention that the post should go to the most senior member of the House. The combine approached the SC to challenge the appointment. Hearing is scheduled for 10:30 am today. ——News Link——– https://timesofindia.indiatimes.com/india/congress-jds-in-sc-over-bjp-mla-made-pro-tem-speaker-hearing-at-1030-am/articleshow/64228740.cms Print Page Previous Next Advertisements ”;
Python – SMTP
Python – SMTP ”; Previous Next Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending an e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon. An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a message. It takes three parameters − The sender − A string with the address of the sender. The receivers − A list of strings, one for each recipient. The message − A message as a string formatted as specified in the various RFCs. Example Here is a simple way to send one e-mail using Python script. Try it once − #!/usr/bin/python3 import smtplib sender = ”[email protected]” receivers = [”[email protected]”] message = “””From: From Person <[email protected]> To: To Person <[email protected]> Subject: SMTP e-mail test This is a test e-mail message. “”” try: smtpObj = smtplib.SMTP(”localhost”) smtpObj.sendmail(sender, receivers, message) print “Successfully sent email” except SMTPException: print “Error: unable to send email” Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and a Subject header, separated from the body of the e-mail with a blank line. To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail). If you are not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you are using a webmail service (such as gmail or Yahoo! Mail), your e-mail provider must have provided you with the outgoing mail server details that you can supply them, as follows − mail = smtplib.SMTP(”smtp.gmail.com”, 587) Sending an HTML e-mail using Python When you send a text message using Python, then all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to the HTML syntax. However, Python provides an option to send an HTML message as actual HTML message. While sending an e-mail message, you can specify a Mime version, content type and the character set to send an HTML e-mail. Example Following is an example to send the HTML content as an e-mail. Try it once − #!/usr/bin/python3 import smtplib message = “””From: From Person <[email protected]> To: To Person <[email protected]> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b> <h1>This is headline.</h1> “”” try: smtpObj = smtplib.SMTP(”localhost”) smtpObj.sendmail(sender, receivers, message) print “Successfully sent email” except SMTPException: print “Error: unable to send email” Print Page Previous Next Advertisements ”;
Python – FTP
Python – FTP ”; Previous Next FTP or File Transfer Protocol is a well-known network protocol used to transfer files between computers in a network. It is created on client server architecture and can be used along with user authentication. It can also be used without authentication but that will be less secure. FTP connection which maintains a current working directory and other flags, and each transfer requires a secondary connection through which the data is transferred. Most common web browsers can retrieve files hosted on FTP servers. The Methods in FTP class In python we use the module ftplib which has the below required methods to list the files as we will transfer the files. Method Description pwd() Current working directory. cwd() Change current working directory to path. dir([path[,…[,cb]]) Displays directory listing of path. Optional call-back cb passed to retrlines(). storlines(cmd, f) Uploads text file using given FTP cmd – for example, STOR file name. storbinary(cmd,f[, bs=8192]) Similar to storlines() but is used for binary files. delete(path) Deletes remote file located at path. mkd(directory) Creates remote directory. exception ftplib.error_temp Exception raised when an error code signifying a temporary error (response codes in the range 400–499) is received.. exception ftplib.error_perm Exception raised when an error code signifying a permanent error (response codes in the range 500–599) is received.. connect(host[, port[, timeout]]) Connects to the given host and port. The default port number is 21, as specified by the FTP protocol.. quit() Closes connection and quits. Below are the examples of some of the above methods. Listing the Files The below example uses anonymous login to the ftp server and lists the content of the current directory. It treates through the name of the files and directories and stores them as a list. Then prints them out. import ftplib ftp = ftplib.FTP(“ftp.nluug.nl”) ftp.login(“anonymous”, “ftplib-example-1”) data = [] ftp.dir(data.append) ftp.quit() for line in data: print “-“, line 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 Changing the Directory The below program uses the cwd method available in the ftplib module to change the directory and then fetch the required content. import ftplib ftp = ftplib.FTP(“ftp.nluug.nl”) ftp.login(“anonymous”, “ftplib-example-1″) data = [] ftp.cwd(”/pub/”) change directory to /pub/ ftp.dir(data.append) ftp.quit() for line in data: print “-“, line When we run the above program, we get the following output − – lrwxrwxrwx 1 504 450 14 Nov 02 2007 FreeBSD -> os/BSD/FreeBSD – lrwxrwxrwx 1 504 450 20 Nov 02 2007 ImageMagick -> graphics/ImageMagick – lrwxrwxrwx 1 504 450 13 Nov 02 2007 NetBSD -> os/BSD/NetBSD – lrwxrwxrwx 1 504 450 14 Nov 02 2007 OpenBSD -> os/BSD/OpenBSD – -rw-rw-r– 1 504 450 932 Jan 04 2015 README.nluug – -rw-r–r– 1 504 450 2023 May 03 2005 WhereToFindWhat.txt – drwxr-sr-x 2 0 450 4096 Jan 26 2008 av – drwxrwsr-x 2 0 450 4096 Aug 12 2004 comp Fetching the Files After getting the list of files as shown above, 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. Print Page Previous Next Advertisements ”;
Python – Remote Procedure Call ”; Previous Next Remote Procedure Call (RPC) system enables you to call a function available on a remote server using the same syntax which is used when calling a function in a local library. This is useful in two situations. You can utilize the processing power from multiple machines using rpc without changing the code for making the call to the programs located in the remote systems. The data needed for the processing is available only in the remote system. So in python we can treat one machine as a server and another machine as a client which will make a call to the server to run the remote procedure. In our example we will take the localhost and use it as both a server and client. Running a Server The python language comes with an in-built server which we can run as a local server. The script to run this server is located under the bin folder of python installation and named as classic.py. We can run it in the python prompt and check its running as a local server. python bin/classic.py When we run the above program, we get the following output − INFO:SLAVE/18812:server started on [127.0.0.1]:18812 Running a Client Next we run the client using the rpyc module to execute a remote procedure call. In the below example we execute the print function in the remote server. import rpyc conn = rpyc.classic.connect(“localhost”) conn.execute(“print(”Hello from Tutorialspoint”)”) When we run the above program, we get the following output − Hello from Tutorialspoint Expression Evaluation through RPC Using the above code examples we can use python’s in-built functions for execution and evaluation of expressions through rpc. import rpyc conn = rpyc.classic.connect(“localhost”) conn.execute(”import math”) conn.eval(”2*math.pi”) When we run the above program, we get the following output − 6.283185307179586 Print Page Previous Next Advertisements ”;
Python – Proxy Server
Python – Proxy Server ”; Previous Next Proxy servers are used to browse to some website through another server so that the browsing remains anonymous. It can also be used to bypass the blocking of specific IP addresses. We use the urlopen method from the urllib module to access the website by passing the proxy server address as a parameter. Example In the below example we use a proxy address to access the website twitter.con for anonymous access. The response status of OK proves the successful access through a proxy server. import urllib URL = ”https://www.twitter.com” PROXY_ADDRESS = “265.24.11.6:8080″ if __name__ == ”__main__”: resp = urllib.urlopen(URL, proxies = {“http” : PROXY_ADDRESS}) print “Proxy server returns response headers: %s ” %resp.headers When we run the above program, we get the following output − Proxy server returns response headers: cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 content-length: 145960 content-type: text/html;charset=utf-8 date: Mon, 02 Jul 2018 02:06:19 GMT expires: Tue, 31 Mar 1981 05:00:00 GMT last-modified: Mon, 02 Jul 2018 02:06:19 GMT pragma: no-cache server: tsa_n set-cookie: fm=0; Expires=Mon, 02 Jul 2018 02:06:10 GMT; Path=/; Domain=.twitter.com; Secure; HTTPOnly set-cookie: _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCAifvVhkAToMY3NyZl9p%250AZCIlNzlhY2ZhMzdmNGFkYTU0ZTRmMDkxODRhNWNiYzI0MDI6B2lkIiUyZTgy%250AOTAyYjY4NTBkMzE3YzNjYTQwNzZjZDhhYjZhMQ%253D%253D–6807256d74a01129a7b0dcf383f56f169fb8a66c; Path=/; Domain=.twitter.com; Secure; HTTPOnly set-cookie: personalization_id=”v1_iDacJdPWUx3e81f8UErWTA==”; Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com set-cookie: guest_id=v1%3A153049717939764273; Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com set-cookie: ct0=50c8b59b09322825cd750ea082e43bdc; Expires=Mon, 02 Jul 2018 08:06:19 GMT; Path=/; Domain=.twitter.com; Secure status: 200 OK strict-transport-security: max-age=631138519 x-connection-hash: 89dfbab81abc35dd8f618509c6171bd3 x-content-type-options: nosniff x-frame-options: SAMEORIGIN x-response-time: 312 x-transaction: 007b998000f86eaf x-twitter-response-tags: BouncerCompliant x-ua-compatible: IE=edge,chrome=1 x-xss-protection: 1; mode=block; report=https://twitter.com/i/xss_report 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 ”;