Python Network Programming Tutorial

Python – RSS Feed

Python &-8211; RSS Feed &-8211; this Article or News was published on this date:2019-05-13 12:42:05 kindly share it with friends if you find it helpful

Python &-8211; RSS Feed


Advertisements


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


Advertisements

Python &-8211; Network Programming

Python &-8211; Network Introduction

Python &-8211; Network Environment

Python &-8211; Internet Protocol

Python &-8211; IP Address

Python &-8211; DNS Lookup

Python &-8211; Routing

Python &-8211; HTTP Requests

Python &-8211; HTTP Response

Python &-8211; HTTP Headers

Python &-8211; Custom HTTP Requests

Python &-8211; Request Status Codes

Python &-8211; HTTP Authentication

Python &-8211; HTTP Data Download

Python &-8211; Connection Re-use

Python &-8211; Network Interface

Python &-8211; Sockets Programming

Python &-8211; HTTP Client

Python &-8211; HTTP Server

Python &-8211; Building URLs

Python &-8211; WebForm Submission

Python &-8211; Databases and SQL

Python &-8211; Telnet

Python &-8211; Email Messages

Python &-8211; SMTP

Python &-8211; POP3

Python &-8211; IMAP

Python &-8211; SSH

Python &-8211; FTP

Python &-8211; SFTP

Python &-8211; Web Servers

Python &-8211; Uploading Data

Python &-8211; Proxy Server

Python &-8211; Directory Listing

Python &-8211; Remote Procedure Call

Python &-8211; RPC JSON Server

Python &-8211; Google Maps

Python &-8211; RSS Feed

Python TP &-8211; Quick Guide

Python TP- Useful Resources

Python TP &-8211; Discussion

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Python – Google Maps

Python &-8211; Google Maps &-8211; this Article or News was published on this date:2019-05-13 12:42:04 kindly share it with friends if you find it helpful

Python &-8211; Google Maps


Advertisements


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


Advertisements

Python &-8211; Network Programming

Python &-8211; Network Introduction

Python &-8211; Network Environment

Python &-8211; Internet Protocol

Python &-8211; IP Address

Python &-8211; DNS Lookup

Python &-8211; Routing

Python &-8211; HTTP Requests

Python &-8211; HTTP Response

Python &-8211; HTTP Headers

Python &-8211; Custom HTTP Requests

Python &-8211; Request Status Codes

Python &-8211; HTTP Authentication

Python &-8211; HTTP Data Download

Python &-8211; Connection Re-use

Python &-8211; Network Interface

Python &-8211; Sockets Programming

Python &-8211; HTTP Client

Python &-8211; HTTP Server

Python &-8211; Building URLs

Python &-8211; WebForm Submission

Python &-8211; Databases and SQL

Python &-8211; Telnet

Python &-8211; Email Messages

Python &-8211; SMTP

Python &-8211; POP3

Python &-8211; IMAP

Python &-8211; SSH

Python &-8211; FTP

Python &-8211; SFTP

Python &-8211; Web Servers

Python &-8211; Uploading Data

Python &-8211; Proxy Server

Python &-8211; Directory Listing

Python &-8211; Remote Procedure Call

Python &-8211; RPC JSON Server

Python &-8211; Google Maps

Python &-8211; RSS Feed

Python TP &-8211; Quick Guide

Python TP- Useful Resources

Python TP &-8211; Discussion

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Python – RPC JSON Server

Python &-8211; RPC JSON Server &-8211; this Article or News was published on this date:2019-05-13 12:42:03 kindly share it with friends if you find it helpful

Python &-8211; RPC JSON Server


Advertisements


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}]]


Advertisements

Python &-8211; Network Programming

Python &-8211; Network Introduction

Python &-8211; Network Environment

Python &-8211; Internet Protocol

Python &-8211; IP Address

Python &-8211; DNS Lookup

Python &-8211; Routing

Python &-8211; HTTP Requests

Python &-8211; HTTP Response

Python &-8211; HTTP Headers

Python &-8211; Custom HTTP Requests

Python &-8211; Request Status Codes

Python &-8211; HTTP Authentication

Python &-8211; HTTP Data Download

Python &-8211; Connection Re-use

Python &-8211; Network Interface

Python &-8211; Sockets Programming

Python &-8211; HTTP Client

Python &-8211; HTTP Server

Python &-8211; Building URLs

Python &-8211; WebForm Submission

Python &-8211; Databases and SQL

Python &-8211; Telnet

Python &-8211; Email Messages

Python &-8211; SMTP

Python &-8211; POP3

Python &-8211; IMAP

Python &-8211; SSH

Python &-8211; FTP

Python &-8211; SFTP

Python &-8211; Web Servers

Python &-8211; Uploading Data

Python &-8211; Proxy Server

Python &-8211; Directory Listing

Python &-8211; Remote Procedure Call

Python &-8211; RPC JSON Server

Python &-8211; Google Maps

Python &-8211; RSS Feed

Python TP &-8211; Quick Guide

Python TP- Useful Resources

Python TP &-8211; Discussion

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Python – Remote Procedure Call

Python &-8211; Remote Procedure Call &-8211; this Article or News was published on this date:2019-05-13 12:42:02 kindly share it with friends if you find it helpful

Python &-8211; Remote Procedure Call


Advertisements


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


Advertisements

Python &-8211; Network Programming

Python &-8211; Network Introduction

Python &-8211; Network Environment

Python &-8211; Internet Protocol

Python &-8211; IP Address

Python &-8211; DNS Lookup

Python &-8211; Routing

Python &-8211; HTTP Requests

Python &-8211; HTTP Response

Python &-8211; HTTP Headers

Python &-8211; Custom HTTP Requests

Python &-8211; Request Status Codes

Python &-8211; HTTP Authentication

Python &-8211; HTTP Data Download

Python &-8211; Connection Re-use

Python &-8211; Network Interface

Python &-8211; Sockets Programming

Python &-8211; HTTP Client

Python &-8211; HTTP Server

Python &-8211; Building URLs

Python &-8211; WebForm Submission

Python &-8211; Databases and SQL

Python &-8211; Telnet

Python &-8211; Email Messages

Python &-8211; SMTP

Python &-8211; POP3

Python &-8211; IMAP

Python &-8211; SSH

Python &-8211; FTP

Python &-8211; SFTP

Python &-8211; Web Servers

Python &-8211; Uploading Data

Python &-8211; Proxy Server

Python &-8211; Directory Listing

Python &-8211; Remote Procedure Call

Python &-8211; RPC JSON Server

Python &-8211; Google Maps

Python &-8211; RSS Feed

Python TP &-8211; Quick Guide

Python TP- Useful Resources

Python TP &-8211; Discussion

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who

Python – Directory Listing

Python &-8211; Directory Listing &-8211; this Article or News was published on this date:2019-05-13 12:42:01 kindly share it with friends if you find it helpful

Python &-8211; Directory Listing


Advertisements


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/
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


Advertisements

Python &-8211; Network Programming

Python &-8211; Network Introduction

Python &-8211; Network Environment

Python &-8211; Internet Protocol

Python &-8211; IP Address

Python &-8211; DNS Lookup

Python &-8211; Routing

Python &-8211; HTTP Requests

Python &-8211; HTTP Response

Python &-8211; HTTP Headers

Python &-8211; Custom HTTP Requests

Python &-8211; Request Status Codes

Python &-8211; HTTP Authentication

Python &-8211; HTTP Data Download

Python &-8211; Connection Re-use

Python &-8211; Network Interface

Python &-8211; Sockets Programming

Python &-8211; HTTP Client

Python &-8211; HTTP Server

Python &-8211; Building URLs

Python &-8211; WebForm Submission

Python &-8211; Databases and SQL

Python &-8211; Telnet

Python &-8211; Email Messages

Python &-8211; SMTP

Python &-8211; POP3

Python &-8211; IMAP

Python &-8211; SSH

Python &-8211; FTP

Python &-8211; SFTP

Python &-8211; Web Servers

Python &-8211; Uploading Data

Python &-8211; Proxy Server

Python &-8211; Directory Listing

Python &-8211; Remote Procedure Call

Python &-8211; RPC JSON Server

Python &-8211; Google Maps

Python &-8211; RSS Feed

Python TP &-8211; Quick Guide

Python TP- Useful Resources

Python TP &-8211; Discussion

UPSC IAS Exams Notes

Developer&-8217;s Best Practices

Questions and Answers

Effective Resume Writing

HR Interview Questions

Computer Glossary

Who is Who