Sign in to follow this  
musafir2007

Help with Python please.. (Scripting)

Recommended Posts

Hey guys I am trying to make a simple converting a number from base 10 to base 16. I am using converttohex.py to process the data filled in the form created by the html. But when I click on "Convert to Hex!" in the web browser, all it gives me is the python code in the next page (not the processed information!). Please Help! thanks! base16.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>A put a HEX on you</title>
</head>
<body>

<form action="converttohex.py" >
<p> This form will convert your age from base 10 (decimal) to base 16 (hexadecimal) </p>
<p> What is your name: <input type="text" name="yourname" /></p>
<p> What is your age :  <input type="text" name="yourage" size="3" maxlength="3" /></p>
<p> <input type="submit" value="Convert to Hex!" name="button1" /></p>
</form>

</body>
</html>
converttohex.py
import cgi
form = cgi.FieldStorage()
# convert an integer that is between 0 and 15 into a hexadecimal character
def strhex(x): 
  if x < 10:
    return str(x)
  elif x == 10:
    return "A"
  elif x == 11:
    return "B"
  elif x == 12:
    return "C"
  elif x == 13:
    return "D"
  elif x == 14:
    return "E"
  else: # must be 15
    return "F"

print """Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x
html1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Your age in hex</title>
</head>
<body>
"""
age = int(form["yourage"].value)
first_digit = age / 16  # what integer do you get when dividing age by 16
second_digit = age % 16 # and what remainder do you get
print "<p>" 
print form["yourname"].value + " has a hex age of " 
# print str(first_digit) + str(strhex(second_digit)) # what I originally had
# which was then modified
print "\"" + strhex(first_digit) + strhex(second_digit) + "\""
print "</p>"

print """
<hr class="footsep" />
<p class="footlink">Return to <a href="./">the index</a>.</p>
</body>
</html>

Share this post


Link to post
Share on other sites
You can solve the conversion more elegantly by using a lookup table:

hextable = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
def ToHex(n):
return hextable[n]



As for your problem, the only thing I can see is a missing """ at the end of the file. Are you sure you've set up your server to handle .py files correctly?


@Jroggy: In Python, """ can be used as a string delimiter.

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
You can solve the conversion more elegantly by using a lookup table:
*** Source Snippet Removed ***

As for your problem, the only thing I can see is a missing """ at the end of the file. Are you sure you've set up your server to handle .py files correctly?


@Jroggy: In Python, """ can be used as a string delimiter.


THanks for your reply guys. I am kinda new to all this.. how do to set up my computer to handle .py files correctly? thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by musafir2007
Quote:
Original post by Captain P
You can solve the conversion more elegantly by using a lookup table:
*** Source Snippet Removed ***

As for your problem, the only thing I can see is a missing """ at the end of the file. Are you sure you've set up your server to handle .py files correctly?


@Jroggy: In Python, """ can be used as a string delimiter.


THanks for your reply guys. I am kinda new to all this.. how do to set up my computer to handle .py files correctly? thanks!


What web server are you using?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this