Search This Blog

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, February 27, 2010

Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Developed four years ago by a fast-moving online-news operation, Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly.

Saturday, October 24, 2009

Send an HTML email with embedded image and plain text alternate

Recipe 473810: Send an HTML email with embedded image and plain text alternate




HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.

Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.

This recipe sends a short HTML message with a single embedded image and an alternate plain text message.





Python
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Define these once; use them twice!
strFrom = 'from@example.com'
strTo = 'to@example.com'

# Create the root message and fill in the from, to, and subject headers

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'

msgRoot['From'] = strFrom
msgRoot['To'] = strTo

msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')

msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')

msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')

msgAlternative.attach(msgText)

# This example assumes the image is in the current directory
fp = open('test.jpg', 'rb')

msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.example.com')

smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())

smtp.quit()




Discussion



While the practice of embedding images within the message provides a better experience for many users than linking to web-hosted images does it is important to consider the impact this has on message size and consequently on message download time.

An alternative implementation for sending HTML messages is provided in this recipe http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67083, though it doesn't address embedding images and was written before the email package was incorporated into Python. The email package and its MIME-handling capabilities significantly reduce the amount of code required.



Thursday, February 12, 2009

IP Address Range Function in Python

def ipAddrRange(startAddr, endAddr):
 def incrAddr(addrList):
    addrList[3] += 1
    for i in (3,2,1):
      if addrList[i] == 256:
         addrList[i] = 0
         addrList[i-1] += 1
 def asString(addrList):
     return ".".join(map(str,addrList))
 startAddrList = map(int,startAddr.split("."))
 endAddrList = map(int,endAddr.split("."))
 curAddrList = startAddrList[:]
 yield asString(curAddrList)
 for i in range(4):
     while curAddrList[i] < endAddrList[i]:
         incrAddr(curAddrList)
         yield asString(curAddrList)

for addr in ipAddrRange("10.255.255.250","11.0.0.20"):
print addr


Tuesday, February 10, 2009

Regular Expression

Regular Expression is really cool, All should know it;) Initially you might feel that its too complex but when u get into it u feel its look complex but it is really cool and easy to use. It completes big task in few lines....... Its really great;) Below is my first function in Python which i have used to search for a String using '*' to escape and search in string. Cheers Python B) def SearchString(pattern,string): ulist = pattern.split('*') if len(ulist) > 1: k = '^' + ulist[0] + '[\w|\_|\-]*' + ''.join([ i + '[\w|\_|\-]*' for i in ulist[1:-1]]) + ulist[-1] + '$' print k if re.search(k,user) != None: return "Found" else: return "Not Found" elif ulist == string: return "Found" else: return "Not Found" >>> SearchUser('vin*jda*is','vinojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vi**jda*is','vinojdavis') ^vi[\w|\_|\-]*[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Found' >>> SearchUser('vin*jda*is','viojdavis') ^vin[\w|\_|\-]*jda[\w|\_|\-]*is$ 'Not Found'

Tuesday, January 27, 2009

Python code for an Ip range

def ipAddrRange(startAddr, endAddr):
   def incrAddr(addrList):
       addrList[3] += 1
       for i in (3,2,1):
           if addrList[i] == 256:
               addrList[i] = 0
               addrList[i-1] += 1

   def asString(addrList):
       return ".".join(map(str,addrList))

   startAddrList = map(int,startAddr.split("."))
   endAddrList = map(int,endAddr.split("."))

   curAddrList = startAddrList[:]
   yield asString(curAddrList)
   for i in range(4):
       while curAddrList[i] < endAddrList[i]:
           incrAddr(curAddrList)
           yield asString(curAddrList)

for addr in ipAddrRange("10.255.255.250","11.0.0.20"):
   print addr



Friday, October 31, 2008

How to write & read a dictionary in File

Using Pickle in Python for dumping and reading a Dictionary from a file



D1 = {'I':1,'II':2,'III':3,'IV':4,'V':5,'VI':6,'VII':7,'VIII':8,'IX':9,'X':10}

# to save a Python object like a dictionary to a file
# and load it back intact you have to use the pickle module

import pickle

print "The original dictionary:"
print romanD1

file = open("abc.dat", "w")
pickle.dump(D1, file)
file.close()

# now load the dictionay object back from the file ...
file = open("abc.dat", "r")
D2 = pickle.load(file)
file.close()

print "Dictionary after pickle.dump() and pickle.load():"
print D2

Tuesday, October 7, 2008

Sockets In Python

Sockets in Python

Sockets are the lead pipes of computer networks: they let you connect with other devices so that information can flow freely. As you might expect, they're widely used on the Internet. Peyton McCullough explains how to code sockets in Python.

Introduction

One of the must-know features of any language is sockets, and, no, I’m not talking about the ones positioned on your ceiling. While many novice scripters will run away at the sound of the word, sockets in Python are not hard to learn and work with at all. In this article, I will explain the basics and even apply sockets in a real-world situation.

Please note that this article assumes that you have a basic command of the Python language. If not, you can either read the official tutorial or buy a book – or both. If you’re looking for a good book, you should head over to O’Reilly & Associates.

With that stated, let’s get started.

The Anatomy of a Socket

Let’s start off simple: what is a socket? The easiest way to learn is to compare computer sockets to lead pipes. Let’s say you have some sort of machine that has two pipes. One pipe pumps in water and the other pipe pumps out water. This will be your server. Now, imagine you have a simple box that has one pipe going through it. Let’s say that you hook it up to your pump machine. It will receive the water from the machine and then give water back. This will be your client. If you wanted to, you could hook up more machines and pipes to this to form a network of clients and servers.

Sockets are useful. The Web browser that you are viewing this page with is connecting to the server that this page is in by way of a socket. If you were to connect to one of your favorite online games or chat networks, you would be using sockets as well. As you can see, sockets are both both powerful and widely-used.

Using sockets in Python is quite easy. Open up your Python command line, and let’s get to some code.

The first thing we must do is import the socket library:

>>> import socket

The socket library contains all the tools we need to work with sockets. The next thing we need to do is create a socket. This is simple. Execute the following code:

>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

This creates a stream socket. You can also work with datagrams by replacing SOCK_STREAM with SOCK_DGRAM. A socket stream is where there is a constant connection between the client and server that stays alive until it is closed, and both the client and the server know if the connection is still alive. With datagrams, however, that is not the case. The connection is not kept alive, and your data might not even be received. Although datagrams can sound like a bad idea at first, they have their purposes. It might be easier and faster to use datagrams in certain situations.

Writing a Simple Server

Let’s write a simple server. If you’ll remember, a server is anything that receives connections from other computers, clients. Create a new Python file named server.py and insert the following code into it:

import socket mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) mySocket.bind ( ( '', 2727 ) ) mySocket.listen ( 1 ) while True: channel, details = mySocket.accept() print 'We have opened a connection with', details print channel.recv ( 100 ) channel.send ( 'Green-eyed monster.' ) channel.close()

That’s quite a mouthful, so let’s split it up into something we can understand. The first two lines should already look familiar. We create a new socket to use. In the third line, we open port 2727 for connections.

To understand what a port is, let’s go back to our analogy. Picture the pump machine with thousands of pipes leading in and out of it. Each pipe would be a port, and clients would have the option of connecting to different ports. However, each port would be different – some might pump out green water, and others might pump out orange water.

The next line tells our socket to wait, or listen, for clients. Following that, there is an infinite loop. In this loop, we accept client connections, print out the client's address, print out the client's message and finally send a message back, closing the connection when we're done.

While the socket library is good for many things, there are several libraries built on the socket library that make development easier and faster. Say you wanted to connect to the official Python site to retrieve a piece of information. You could accomplish this task with the socket library, but there's no use working at such a low level when other libraries could do this in a shorter amount of lines. There are libraries for HTTP, FTP, Gopher, telnet and more.

A Real-World Problem and a Pythonic Solution

Let's say that you need to convert United States Dollars into Euros. This may seem complex at first, but it really isn't. It can be accomplished with sockets. We'll use the urllib library to connect to 'http://xe.com' and convert ten dollars into Euros.

Open up the Python command line and import the urllib module:

>>> import urllib

Connecting to the currency converter website I mentioned is simple with the urllib library -- a lot simpler than it would have been if I had used the socket library alone. It can be accomplished with the following line of code:

>>> currency = urllib.urlopen ( 'http://xe.com/ucc/convert.cgi?Amount=10&From=USD&To=EUR' ) >>> data = currency.read()

If you attempt to print out the data variable, you will get a good bit of junk that is not necessary in our script. However, here are the key lines:

<TD ALIGN=RIGHT><FONT FACE="Arial,Helvetica"> <FONT SIZE=+1><B>10.00 USD</B></FONT><FONT SIZE=-1><BR>United States Dollars </FONT></TD> <TD> </TD> <TD ALIGN=CENTER><FONT FACE="Arial,Helvetica" SIZE=+3><B>=</B></FONT></TD> <TD> </TD> <TD ALIGN=LEFT><FONT FACE="Arial,Helvetica"> <FONT SIZE=+1><B>7.46213 EUR</B></FONT><FONT SIZE=-1><BR>Euro </FONT></TD>

( Note that the values above are outdated. )

We can use regular expressions to sort through all the junk and take out the figures we need from the above code:

>>> import re >>> euros = re.search ( '(\d*)\.(\d*) EUR', data ) >>> print '10 United States Dollars are equal to...' >>> print euros.group ( 1 ) + '.' + euros.group ( 2 ), 'Euros.'

We also need to clean up and close the connection like we did earlier:

>>> currency.close()

That's it, we've solved our problem in only a few lines! If we were to do it with the socket library, however, it would have required a number of more lines to accomplish the same result.

You have learned that sockets are a valuable tool in the Python scripting language. They enable you to communicate with other computers, clients and servers, and to exchange data. Sockets are used to accomplish many common computer-related tasks, such as browsing Web pages. Though many novices assume that sockets are difficult, the opposite is true. Python also includes a number of libraries built on top of the socket library that allow you to easily write networking applications.

You now have the knowledge and tools to construct scripts that use sockets. Where you choose to go from here is entirely up to you. However, you may wish to browse through the standard library to find modules to aid development using sockets. Some modules that may help you include the select, asyncore, asynchat, ftplib ( FTP ), poplib ( mail ), imaplib ( mail ), smtplib ( mail ), telnetlib ( telnet ), nnttplib ( news ), SocketServer ( server ), BaseHTTPServer ( HTTP server ), SimpleHTTPServer ( HTTP server ) and CGIHTTPServer ( HTTP server ) modules.