views:

282

answers:

2

Hello,

I was given this Python code that would calculate an MD5 value for any phrase:

import md5
md5.new("Nobody inspects the spammish repetition").digest()

(The phrase here is: "Nobody inspects the spammish repetition")

What I want to do is display this value in my browser. How do I do it in Python?

I tried all these variants, none of them worked:

import md5
show = md5.new("Nobody inspects the spammish repetition").digest()
print show

import md5
print md5.new("Nobody inspects the spammish repetition").digest()

import md5
md5.new("Nobody inspects the spammish repetition").digest()
print md5

import md5
md5.new("Nobody inspects the spammish repetition").digest()
print md5.new

update A:

By now (Monday, 5 April 2010, 07:19:35 GMT) I have received two answers from Ignacio Vazquez-Abrams and from Ji. Both have suggested pretty much the same thing. I have tried Ji's code, but it didn't work. Here is the screen shot of the error lines I received: alt text

(I believe you need to right click on the image and choose "View the Image" to see it in a bigger size)

+3  A: 

.hexdigest() is what you want.

Ignacio Vazquez-Abrams
Thank You, Ignacio. I guess You are suggesting the same thing Ji suggested here right after You (see below). Something went wrong with his code on my computer, and I don't know why (please refer to the "Update A" section above to see the details from the error-message screeen shot).
brilliant
+3  A: 

In order to display the hexdigest in your browser you need to have some sort of web framework (in this case in python) that handles all the web serving for you.

Here's an example using web.py (I've copied the default example and adjusted for the md5). But you can use any other framework out there

import web
from md5 import md5

urls = (
    '/(.*)', 'digest' 
)

app = web.application(urls, globals())

class digest:        
    def GET(self):
        return md5("Nobody inspects the spammish repetition").hexdigest()

if __name__ == "__main__":
    app.run()
Jj
Thank You, Ji, but I just tried Your code and it didn't work. Am I doing something wrong? Please refer to "Update A" above to see the screen shot of my problem.
brilliant
@brilliant: The `web` module won't be used unless you're already using web.py.
Ignacio Vazquez-Abrams
Thank You, Ignacio!!! Can You, please, give me some clue as to how I could start using it?
brilliant
Nope, never used it. I use WSGI or Django myself. http://wsgi.org/ http://djangoproject.com/
Ignacio Vazquez-Abrams
@ Ignacio Vazquez-Abrams: Oh, Lord!!! It has become so complicated! Finally, I have arrived at the necessity of getting myself familiarized with another new big name - Django! I wonder, why does it have to be so complicated? Is it really so hard to just display the calculated MD5 value in my browswer using just Python?
brilliant
Yeah it's a fairly complicated thing to listen for HTTP connections, connect those requests to your application in a consistent way, configure that application to enter your code at a specific point, and then take the return of your code and make it suitable for a web browser. That's why people use frameworks instead of writing these things themselves.
Jesse Dhillon