Feeds:
Posts
Comments

Archive for the ‘Python’ Category

This below program helps to crawl a web page,especially if we are behind the firewall..

Proxy.py

import urllib2
#Browser proxy setting
proxy_info = {
‘user’ : ‘vignesh_v’,
‘pass’ : ‘passwd’,
‘host’ : “10.16.65.13”,
‘port’ : 80
}

# build a new opener that uses a proxy requiring authorization
proxy_support = urllib2.ProxyHandler({“http” : \
http://%(user)s:%(pass)s@%(host)s:%(port)d” % proxy_info})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
# Install the opener
urllib2.install_opener(opener)
#Open the url
f = urllib2.urlopen(‘http://www.python.org/’)
print f.headers
print f.read()

Advertisements

Read Full Post »

Python has inbulit sqlite3 db..This below program will create a table in sqlite3 and insert a record and will retrieve the data.We use Web.py module to display the data in a html file.Web.py module has inbuilt server which will deploy this html..

Please install this webpy module

Note: User have to align the below code..

SmsAppMain.py

#imports the web.py module
import web
#Import inbulit sqlite db
import sqlite3

#Creating the connection
global connection
connection = sqlite3.connect(‘smsapp.db’)
#Load the cursor
global cursor
cursor = connection.cursor()

def createUser():
#Create the usertbl
cursor.execute(”’create table usertbl(username text,passwd text)”’)
print ‘Created Usertbl’
# Insert a row of data
cursor.execute(“””insert into usertbl values (‘viggi’,’test’)”””)
# Save the changes
connection.commit()

def retrieveUser(name):
import sqlite3 as db
db = web.database(dbn=”sqlite”,db=”smsapp.db”)
#Get a reference to the connection object via the cursor
connection = db._db_cursor().connection
cursor.execute(‘select * from usertbl’)
for element in cursor:
strUser = element
return strUser

#look for templates in your templates directory
render = web.template.render(‘templates/’)

#Regex that matches a URL,like /,/help
urls = (“/.*”, “SmsAppMain”)

#Class with GET function
class SmsAppMain:
def GET(self):
name = ‘Viggi’
#createUser()
strUser = retrieveUser(name)
#index is the name of the template
return render.index(str(strUser))

if __name__ == “__main__”:
#Create an application with the URLs
#looking up the classes in the global namespace
app = web.application(urls, globals())
app.run()

index.htm:

$def with (name)
$if name:
hello to $name.
$else:
Hello, dumb

Create the templates folder and copy this index.htm…
cmd : python SmsAppMain.py

Open a browser and hit this url http://localhost:8080/index.htm


References :

http://webpy.org/skeleton/0.3
http://docs.python.org/library/sqlite3.html

Read Full Post »