Thursday, July 31, 2008

Py2exe and email.generator, email.blah blah errors

Create this file :-

import email
msg = email.MIMEText.MIMEText("dsafdafdasfA")
print "ok"

Create setup.py like below :

from distutils.core import setup
import py2exe
setup(console=['testmime.py'])


Run python setup.py py2exe and you will get errors

The following modules appear to be missing['email.Generator', 'email.Iterators', 'email.Utils']

And your program will bombed out with errors like ""Import error : No module name text"


To solve this i just added " print self.__name__" under __init__.py of \email package.
And it seems the LazyImporter is confusing py2exe.

Solution ?

import email
import email.mime.text
import email.iterators
import email.generator
import email.utils

Thursday, July 10, 2008

Python : POP3 SSL Gmail Proxy

There was a friend of mine who went to Macau and his company uses Gmail for hosting corporate emails. He has a local mailserver that would want to download from this gmail and it doesnt support POP3 over SSL (port 995).

So once again Python comes to the rescue, 2 hours later, its done. A generic POP3SSL proxy that will allow any mailserver/client to collect mails from gmail via POP3SSL.

Just run : pop3ssl.exe host port (ur local ip and port that u want to use as proxy)
Example : pop3ssl 10.8.0.1 110 (or if ur mailserver is in the same machine, use another port)

Download it here : pop3ssl.zip

Note although its free for use, pls leave a comment here on how this program helped you.

Wednesday, July 9, 2008

Pythonist Rants

Its interesting to see how Pythonist does some stuffs and then suddenly you find that doing that in some other language wasn't a bad idea either ....and then...suddenly (again) it dawns on you that many things could be done in Python that is IMPOSSIBLE to duplicate exactly .

Lets say you have a vector in C++ that you need to remove an item if it fits a certain scenario.

The layman's way is to do the following in C++ (not using algorightm stl ;-))

vector::iterator ptr;
for (ptr = abc.begin(); ptr != abc.end(); ptr++)
{
if (*ptr == "red")
{
abc.erase(ptr)
if (ptr == abc.end()) break;
ptr--
}
}


This is one of the way Pythonist does it :-


for x in abc[:]: # this means makes a copy of abc
if x == "red" :
acb.remove(x)

Yup, coming from C++, one's mentality one would wonder why we didnt use that similar method in Python in C++. However, this wouldn't work in C++ without futher modifications, this is because the "erase" is not taking a value but rather the iterator which means if you do this on the original vector but you passed in the iterator instance from the copy, it wouldn't work.


Then there is the story of smtpd.py. Its basically a smtp proxy/server skeleton that supports the basic commands. And how does one go and implement the "MAIL FROM", "RCPT TO", "DATA" method? The normal thinking would be to do a method/function for each of these commands and then there is the switch or if-else statement working on a read buffer.

How did smtpd.py do it?

method = getattr(self, 'smtp_' + command, None)
if not method:
self.push('502 Error: command "%s" not implemented' % command)
return
method(arg)

Introspection! It basically appends the command read and massaged from the buffer and append to the "smtp_" and invoke the method ....

Tuesday, July 8, 2008

Python fun project : MSN CHAT bot

I downloaded MSN-lib 3.6 from sourceforge and soon the naughty idea of linking this with Eliza
the therapist (joe, jeff , jez version of Eliza in python) cropped up almost instantly in my head.
So using a skeleton code of using MSN-lib i picked up from some forum, i did 1+ 1 = 2.

To make it realisic, i provide the "first msg" and the victim input before the bot takes full auto drive.

(All credits due to the original author and contributor to msnlib, eliza and related sources)

I must say i had a great time laughing over the chat logs as my friends and peers communicated with my chatbot. I made the program to log to some hardcoded directory of c:\msnbot since some sample codes is logging to some unix paths.

Well, if u need a break and a laugh, here is the chatbot and continue reading to find out how to configure it :-

Open the file : marbot.py, edit the values below and enter ur hotmail login and password.

m.email = "xxxx"
m.pwd = "xxxx"

Also change the name and nick to ur own :-

m.change_nick("-M- The future")

Ok, done now you are ready to go :-

python marbot.py

or just :

marbot.py

After running for a while it will ask 2 lines of input , the first line is the email id of the "victim" you intend to start a conversation with, the 2nd line is the first message you want to send to this person.

From then on, just watch and laugh as your friend communicate with Eliza. The logs are found in c:\msnbot (create this directory or unzip the files below to this directory)

Download the fun bot here