You are on page 1of 4

SSN COLLEGE OF ENGINEERING

RECORD SHEET

Sheet No

Ex.no.02 Date :

RUNNING PYTHON

1.Objective: To write a python script to access a dynamic webpage, retrieve contents and display it based on user input. Also, to mail the retrieved information to the user. 2.Installing python: Navigate to python.org Download python-2.7.tar.gz file. Once the file is downloaded, untar it. Run it by ./install.sh (or) ./configure. 3.Installing Pythons MySQL: Open a new terminal and type the command:- sudo yum -install mysql-5.5-server. Enter the password. Select the option to install all the dependencies. Once MySQL server is installed run the config to initialize the username, password. Click on finish to complete. 4.MySQL database and Table configuration: In the standard installation of MySQL on Linux the tables and their datastructures is stored in /var/lib/mysql. 5. MySQL connectivity: Install the package MySQLdb:- sudo yum install MySQLdb-connector. Connect to the MySQL using:import MySQLdb; def connect(); db=MySQL.connect(host=127.0.0.1,user= root, password= ,db= python) cursor=db.cursor(); query=select * from rating data=cursor.fetchall(); db.close(); print data obj.connect() 6.Design of the Python Application: 6.1 Description: The application retrieves the rating for a movie from a website and displays it to the user. It also updates the rating information in the database. 6.2 Procedure : 1)Mysql connection is established. If connection is not established ,error message is displayed. 2)Proxy is set up using the procedure mentioned above. 3)The source code is retrieved from the website: #download the file: file = urllib2.urlopen('http://www.desimartini.com/movies/ice-age-continental-drift/md1818.htm') 4)the required position in the source code is); identified and the rating for a particular movie is obtained using substr() 5)The retrieved information is then written into the html file which is then rendered to the user.

SSN COLLEGE OF ENGINEERING


RECORD SHEET

Sheet No

7.Program coding: CLIENT : <html> <head> <script type="text/javascript"> function fnnc() { window.location="myfile.html"; } </script> </head> <body><center> <b>ICE AGE 4 MOVIE RATING</b> <form action="foss1.html"> <input type="button" value="click to know rating" onclick="fnnc();"> </form> </center> </body> </html> SERVER : import MySQLdb,urllib2 #proxy = urllib2.ProxyHandler({'http': 'http://proxy.ssn.net:8080'}) #auth = urllib2.HTTPBasicAuthHandler() #opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler) #urllib2.install_opener(opener) import urllib2 #import easy to use xml parser called minidom: from xml.dom.minidom import parseString #all these imports are standard on most modern python implementations import smtplib from email.mime.text import MIMEText sender = 'vandy199204@gmail.com' recipents = raw_input("Enter to address: ") #download the file: file = urllib2.urlopen('http://www.desimartini.com/movies/ice-age-continental-drift/md1818.htm') #convert to string: data = file.read() #close file because we dont need it anymore: file.close() # Open database connection db = MySQLdb.connect("localhost","root","vandhana","DB" ) # prepare a cursor object using cursor() method c=db.cursor(); sql = """INSERT INTO rating values('ICE_AGE','3')""" c.execute(sql) # disconnect from server db.close() nth = data[17711:17712]; nth2='the rating for ice age movie is ' str=nth2+nth; print str

SSN COLLEGE OF ENGINEERING


RECORD SHEET

Sheet No

fo = open("/home/vandhana/Desktop/pythonex/foss.html", "w") fo.write(str); # Close opend file fo.close() msg = MIMEText(str) msg['Subject'] = 'foss mail' msg['From'] = sender msg['To'] = recipents smtpserver = 'smtp.gmail.com' smtpuser = 'vandy199204' # set SMTP username here smtppass = 'vandhana1992' # set SMTP password here session = smtplib.SMTP("smtp.gmail.com", 587) session.ehlo() session.starttls() session.ehlo() session.login(smtpuser, smtppass) smtpresult = session.sendmail(sender, [recipents], msg.as_string()) session.close()

8.Output Snapshot: TERMINAL :

CLIENT :

SERVER :

SSN COLLEGE OF ENGINEERING


RECORD SHEET

Sheet No

MAIL :

DATABASE :

9.Conclusion:

In fedora environment, MySQL , Python were installed. A python file is created to retrieve the rating for a movie from a website "http://www.desimartini.com/movies/ice-age-continentaldrift/md1818.htm". The rating is retrieved and is written to the html file and is then updated in the database and the retrieved data is mailed to the desired user using SMTP.

You might also like