Jul 31

Zlib compression and Decompression in Python

a=”ASDADASDASDASDASD”

import zlib
c = zlib.compress(a)

#to decompress

d=zlib.decompress(c)

//Bz2 Compression and Decompression in Python

import bz2
c =bz2.compress(a)

#to decompress

d=bz2.decompress(c)

Jul 31

 Base64 encoding

import base64

page=”spmethingsadasd”

a=base64.b64encode(the_page)

print a

Base64 decoding in Python:

import base64

b=base64.b64decode(a)

print b

Jul 29

#!C:/Python25/python.exe -u
import MySQLdb

print “Content-type: text/html”
db=MySQLdb.connect(host=”localhost”,user=”xxxxx”,passwd=”xxxxxx”,db=”xxxxx”)
cursor=db.cursor()

import socket
timeout=1
a=1
socket.setdefaulttimeout(timeout)
from urllib2 import Request, urlopen, URLError, HTTPError

cursor.execute(’SELECT * FROM `stringf` LIMIT 20,5′)
numrows = int(cursor.rowcount)
# get and display one row at a time
x=20
for x in range(20,25):
row = cursor.fetchone()
print row[0], “–>”, row[1]
url=’http://www.’+row[1]+”
req = Request(url)
try:
response = urlopen(req)
except:
print ‘error’

else:
try:
the_page=response.read()
b=row[1]+’.txt’
f=open(b,’w')
f.write(the_page)
f.close()
except:
print ‘error’

Jul 28

Source code for writing contents to file in Python

file=open(’2.txt’,'w’)

file.write(’asdasdadas’)

file.close

Jul 28

Source code for reading a file in Python:

file=open(’2.txt’,'r’)

file1=file.read()

file.close

to see the contents of the read  file simply use

print file1

Jul 23

Below is the code which will allow you to create folders/directories in Python

import os

os.makedirs(’C:/Python25/testing/a/dddf/’,mode=666)

This will create a directory called dddf under a and it will have permissions 666

Jul 14

Here is the simple example of for loop in python   Code: //This line is important,This is where you declare how many times the loop should be executed in range,To execute the loop ten times you declare range(1,10): 
for x in range(1,10):
 
print x Output: In the above loop x is the variable… and then range can be replaced by array  so you do want to specify the length of the array when you are going to use array values in the for loop 123456789  

Jul 02


After installing active state python just go to

Start->Programs->Active state Active Python 2.5 ->Interactive Shell or Python Win Editor…

You can choose Python Win Editor which is of GUI mode..

A Window will open in front of you a command prompt “>>> ”

After just enter         print “My First Python Program”

and then press enter button on Keyboard  your result will displayed in the next line on python editor

This   is one of the simplest programs in Python and it is one of the fastest Languages. in terms of speed.

Also we can run these on any Server provided Python is installed in it….

Jun 25

Python is a wonderful language such that once you learn it you will say it as the best one as it’s faster than many other applications..

So how to try python

Here is the link that will give you step by step description how to install python your PC and also how to install it on local server

http://www.richarddooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/

and this link will tel you

how to configure a httpd file for Cgi-bin to run python scripts on your local server