Using filepath method to identify an .html page

Dave Angel d at davea.name
Tue Jan 22 07:29:21 EST 2013


On 01/22/2013 07:02 AM, Ferrous Cranus wrote:
> # ====================================================================================================================================
> # produce a hash string based on html page's filepath and convert it to an integer, that will then be used to identify the page itself
> # ====================================================================================================================================
>
> pin = int( hashlib.md5( htmlpage ) )
>
> This fails. why?
>
> htmlpage = a string respresenting the absolute path of the requested .html file
> hashlib.md5( htmlpage ) = conversion of the above string to a hashed string
> int( hashlib.md5( htmlpage ) ) = conversion of the above hashed string to a number
>
> Why this fails?
>

Is your copy/paste broken?  It could be useful to actually show in what 
way it "fails."

The md5 method produces a "HASH object", not a string.  So int() cannot 
process that.

To produce a digest string from the hash object, you want to call 
hexdigest() method.  The result of that is a hex literal string.  So you 
cannot just call int() on it, since that defaults to decimal.

To convert a hex string to an int, you need the extra parameter of int:

    int(mystring, 16)

Now, see if you can piece it together.


-- 
DaveA



More information about the Python-list mailing list