Python
From Site5Wiki
Python is a programming language used in building both websites and standalone programs.
Get more info at Python.org
Subpages:
Contents |
[edit] Python on Site5
-
Current version installed on our server fleet
[edit] Using Python in a Site5 site
[edit] Write the script
- Your script must start with a line that tells the server where to find Python:
- either: #! /usr/local/bin/python
- or: #! /usr/bin/env python
- If you want your script to be executable from a web browser (as in, http://mydomain.com/cgi-bin/test.py) the first 2 lines must say that it's a web page or text file
- the first line must be one of these 2 (the difference is that a web page will require a <br> to go to the next line, and ignore tabs; a plain text file will take a \n to go to the next line, and a tab will actually tab the text that is seen):
- print "Content-type: text/html\n" # The content is a web page
- print "Content-type: text\n" # The content is a plain text file
- The second line is required by good web standards, and the newest FireFox balks if you don't use it
- print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
- the first line must be one of these 2 (the difference is that a web page will require a <br> to go to the next line, and ignore tabs; a plain text file will take a \n to go to the next line, and a tab will actually tab the text that is seen):
For example:
#! /usr/local/bin/python def Main(): #------------------ # Main procedure print "Content-type: text/html\n" print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' print 'Hello world!' #------------------ # Main Main()
[edit] Upload the script
- Upload your script as text, not as binary
- Hint: in your FTP application, add 'py' in the list of extensions that must be uploaded as text
- Your script must be placed in this directory:
- /public_html/cgi-bin/
- Change the permissions of your script file so that it may be executed by anyone:
- either 755 (everyone can execute, everyone can look at the text of the script itself)
- or 711 (everyone can execute, people cannot look at the text of the script itself - best if there's a password embedded in the script)
[edit] Execute your script
- You may run your script manually either from a web browser or from the command line
- In a web browser, go to its URL: http://mydomain.com/cgi-bin/test.py
- The output will show up on the web page
- In a telnet client (such as PuTTY), after logging in, type: /usr/local/bin/python ~/public_html/cgi-bin/test.py
- The output will show up on the telnet client's window
- In a web browser, go to its URL: http://mydomain.com/cgi-bin/test.py
- Your script may also be run at regular internals by a cron job
[edit] Troubleshoot the script
- When executing the script from a web browser
- If you get a 404 error (page not found):
- the file name you typed in the URL and the name of the file are different
- the file is not in the cgi-bin folder, or the URL is not referring to that folder
- If you get a 403 error (Forbidden):
- You didn't set the file's execution permissions correctly
- The server doesn't know that the script needs to be executed, and the read permissions are off
- If you get a 500 error (Internal Server Error):
- You uploaded the script as binary instead of as text
- You didn't set the file's execution permissions correctly
- You made a mistake on that first line that tells where to find Python
- There's a Python syntax error, such as wrong tabbing, missing ':' after an if, missing included file
- suggestion: try running the script with a Python on your own computer, to see where the error is
- Suggestion: look at the error log, in your siteadmin page (http://yourdomain.com/siteadmin/), Stats and web logs, Error Logs. Sometimes there's a hint to the problem in that log
- If you see the contents of the script (instead of the intended output)
- The server doesn't know that the script needs to be executed. It may be in a directory other than cgi-bin
- If you see nothing at all
- The script may not be trying to do anything at all, or may be ending prematurely
- Try putting a print 'test' in it, and see if you see 'test' on the output
- The script may not be trying to do anything at all, or may be ending prematurely
- If you get a 404 error (page not found):
- Seeing the traceback in case of error
- Usually, tracebacks go to the error log: in your siteadmin page (http://yourdomain.com/siteadmin/), Stats and web logs, Error Logs
- To see them in the web browser (much more convenient!) add these lines to your script
... import sys # Import the modules that let us deal with the destination for error messages ... # Direct errors to the web page origStdErr = sys.stderr # Save the original destination for error messages sys.stderr = sys.stdout # Change the destination for error messages to the web page or command line ... # Restore the destination for the errors sys.stderr = origStdErr # Retrieve the original destination for error messages and start using it again
[edit] MySQL and Python in a shared account
Python and MySQL together can be very powerful. Unfortunately, the standard release of Python doesn't come with the tools to access a MySQL database. Those tools are part of the MySQLdb module, which must be installed separately. Unfortunately, as a user of a shared account, you are not allowed to install modules onto your server, because that would affect all the users sharing your server. Only the Site5 administrators may install that module for you. The fact is that installing modules for you is not a task that you can expect them to do: it's not part of the service that Site5 agreed to provide. So, open a service ticket, and ask your administrators very nicely if pretty please they would install the MySQLdb module on your server. If you're lucky, your request will go to an administrator who is willing to go above the call of duty and help you. Thank them profusely, as it's really not what they are paid to do.
Watch out though: the next time your server crashes and is rebuilt, the MySQLdb module will be gone, and executing the script will result in a 500 error (Internal Server Error). At that point, you'll have to go back to the administrators and ask you to please help you again.
To use the MySQLdb module, do this:
import MySQLdb allRecordsSQL = """ SELECT * FROM table_name """ oneRecordSQL= """ SELECT * FROM table_name WHERE id='1234' """ def Main(): #------------------ # Main procedure ... dbConnection = MySQLdb.connect(passwd='mypassword', db='myuseraccount_web') # Open the database dbCursor = dbConnection.cursor(MySQLdb.cursors.DictCursor) # Create a cursor that returns dictionaries dbCursor.execute(allRecordsSQL) # Get all records in that table listOfRecords = dbCursor.fetchall() # Get a list of the records, one dictionary per record dbCursor.execute(oneRecordSQL) # Get that particular record in that table oneRecord = dbCursor.fetchone() # Get a dictionary for that record dbConnection.close() # Close the database connection ... #------------------ # Main Main() # Do the main procedure
Subpages:
Related wiki pages: Turbogears
[edit] Tools
[edit] Books We Recommend
Python Standard Library, by Fredrik Lundh ISBN 9780596000967
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
For beginners:
Learning Python, 2nd Ed., by Mark Lutz and David Ascher ISBN 9780596002817
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
For beginners:
Python Pocket Reference, 3rd Ed., by Mark Lutz ISBN 9780596009403
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
Python Cookbook, 2nd Ed., by Alex Martelli, Anna Martelli Ravenscroft, and David Ascher ISBN 9780596007973
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
Programming Python: Object-Oriented Scripting, 2nd Ed., by Mark Lutz ISBN 9780596000851
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
[edit] XML and Python
Python & XML, by Christopher A. Jones and Fred L. Drake, Jr. ISBN 9780596001285
:
- Find this book at
O'Reilly
- Find this book at
Barnes & Noble
- Find this book at
BooksPrice
- Find this book at
InformIT
- Find this book at
Powells' Books
[edit] Free Books
Dive Into Python, by Mark Pilgrim:
Translations:
