Use append to merge two lists in python

lista = [1,2,3] listb = [4,5,6]   mergedlist =[] for elem in lista: mergedlist.append(elem) for elem in listb: mergedlist.append(elem)
Continue reading…

Enjoyed this post? Share it!

 
 

Write xml file using python

from xml.dom.minidom import Document doc = Document() people = doc.createElement("people") doc.appendChild(people) aperson = doc.createElement("person") people.appendChild(aperson) name = doc.createElement("name") aperson.appendChild(name) personname = doc.createTextNode("Annie") name.appendChild(personname) filename = "people.xml" f = open(filename, "w") f.write(doc.toprettyxml(indent=" ")) f.close()
Continue reading…

Enjoyed this post? Share it!

 
 

Read and display data from sqlite table using python

import cgi, os, sys import sqlite3 as db   conn = db.connect(’test.db’) cursor = conn.cursor() conn.row_factory = db.Row cursor.execute("select * from person") rows = cursor.fetchall()   sys.stdout.write("Content-type: text.html\r\n\r\n") sys.stdout.write("") sys.stdout.write("<html><body><p>") for row in rows: sys.stdout.write("%s %s %s" % (row[0],row[1],row[2])) sys.stdout.write("<br />") sys.stdout.write("</p></body></html>")
Continue reading…

Enjoyed this post? Share it!

 
 

Create a simple httpserver in python

import sys import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler   Handler = SimpleHTTPRequestHandler Server = BaseHTTPServer.HTTPServer Protocol = "HTTP/1.0"   if sys.argv[1:]: port = int(sys.argv[1]) else: port = 8000   server_address = (’127.0.0.1′, port)   Handler.protocol_version = Protocol httpd = Server(server_address, Handler)   print("Serving HTTP") httpd.serve_forever()
Continue reading…

Enjoyed this post? Share it!

 
 

Website crawler in python

import urllib, htmllib, formatter, re, sys   url = sys.argv[1] website = urllib.urlopen("http://"+url) data = website.read() website.close() format = formatter.AbstractFormatter(formatter.NullWriter()) ptext = htmllib.HTMLParser(format) ptext.feed(data) links = [] links = ptext.anchorlist for link in links: if re.search(’http’, link) != None: print(link) website = urllib.urlopen(link) data = website.read() website.close() ptext = htmllib.HTMLParser(format) ptext.feed(data) morelinks = ptext.anchorlist for […]
Continue reading…

Enjoyed this post? Share it!

 
 

Use nntplib to read news in python

from nntplib import * s = NNTP(’web.aioe.org’) (resp, count, first, last, name) = s.group(’comp.lang.python’) (resp, subs) = s.xhdr(’subject’, (str(first)+’-‘+str(last))) for subject in subs[-10:]: print(subject) number = input(’Which article do you want to read? ‘) (reply, num, id, list) = s.body(str(number)) for line in list: print(line)
Continue reading…

Enjoyed this post? Share it!

 
 

Read gmail email using python imap

import imaplib   mailserver = imaplib.IMAP4_SSL(’imap.gmail.com’, 993) username = ‘gmailusername’ password = ‘gmailpassword’ mailserver.login(username, password)   status, count = mailserver.select(’Inbox’) status, data = mailserver.fetch(count[0], ‘(UID BODY[TEXT])’)   print data[0][1]   mailserver.close() mailserver.logout()
Continue reading…

Enjoyed this post? Share it!

 
 

Send email using gmail smtp and python

import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText   fromaddr = ‘fromaddr@gmail.com’ toaddr = ‘toaddr@gmail.com’ text = ‘test email message sent from Python code’ username = ‘fromaddruser’ password = ‘fromaddrpassword’ msg = MIMEMultipart() msg[’From’] = fromaddr msg[’To’] = toaddr msg[’Subject’] = ‘Test’ msg.attach(MIMEText(text)) server = smtplib.SMTP(’smtp.gmail.com:587’) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.sendmail(fromaddr, toaddr, […]
Continue reading…

Enjoyed this post? Share it!

 
 

CRUD operations in sqlite using python

import sqlite3 as db   conn = db.connect(’mytest.db’) cursor = conn.cursor() cursor.execute("drop table if exists datecounts") cursor.execute("create table datecounts(date text, count int)") cursor.execute(’insert into datecounts values("12/1/2011",35)’) cursor.execute(’insert into datecounts values("12/2/2011",42)’) cursor.execute(’insert into datecounts values("12/3/2011",38)’) cursor.execute(’insert into datecounts values("12/4/2011",41)’) cursor.execute(’insert into datecounts values("12/5/2011",40)’) cursor.execute(’insert into datecounts values("12/6/2011",28)’) cursor.execute(’insert into datecounts values("12/7/2011",45)’) conn.row_factory = db.Row cursor.execute("select * […]
Continue reading…

Enjoyed this post? Share it!