Lecture 40: Web stuff

posted Apr 15, 2013, 7:03 AM by Samuel Konstantinovich   [ updated Apr 16, 2013, 6:24 AM ]
Part 1: How to put up a file on the web server that will open in a web browser

0. Make a py file: The first three lines are new, and should be exactly as described the rest of the py file prints out a basic html page. 

#!/usr/bin/python
print 'Content-Type: text/html'
print
print '''
<html>
  <head><title>O hai</title></head>
  <body>
    <b>Greetings</b>my fellow cat lover.
  </body>
</html>
'''

1. Test your py file. Does it run? If not fix it. When it runs, it must print the html.

2. Make sure the file is in your public html directory.

3. Make the file 'executeable' there are two ways you can do this. Choose 1 of these options:
3a. Filezilla: on the right side (remote computer where the files are) right click the file, click File Permissions, then check off all three execute boxes.

3b. Terminal.
(If it is a remote system SSH into the computer you want to change) 
Change to the appropriate directory
konstans@lisa:~/$ cd public_html
konstans@lisa:~/public_html$

Change the permissions using chmod
konstans@lisa:~/public_html$chmod +x file.py
konstans@lisa:~/public_html$

4. Skip this step if you created the py file in class, or on a mac/linux computer. 
If you created the file on windows you have this extra step. 
Because windows uses  \r\n for a newline, but on windows the \r is hiden.
You need to replace \r\n with just \n once you put it on the server.

4a. If you are already in the computer lab you can use Gedit to fix your files. Gedit has an option when you save-as (the top file bar has the save-as option), change the file type from windows to linux/unix.

4b. You can also replace them in python by running a python program on them. Download fix.py from the bottom of the page and put it in your public_html directory.

Lets say you upload a file 'yourpage.py' and need to fix it

SSH into the computer you want to change the file. 
Change to the appropriate directory
konstans@lisa:~/$ cd public_html
then run your py file as follows:
konstans@lisa:~/public_html$ python fix.py yourpage.py
konstans@lisa:~/public_html$

This has just fixed the file, but you must run fix.py every time you upload the file from a windows source

5. Connect to the web server and open the py file.



Part II: Make html using a py file and post it

Example:
#!/usr/bin/python
print 'Content-Type: text/html'
print
#import modules and define your functions
#after the first three lines

print '''
<html>
  <head><title>My webpage</title></head>
  <body>'''
print 'This is a plain text body, that can be replaced by something else'
print '''
  </body>
</html>
'''

Goal:
Use your madlibs python code to make a madlibs website. Feel free to include images, colors, and other html tags. (Remember your madlibs is stored on the hw server if you don't have it with you)
-Format the text so it is easy to read.
-Try font effects to make your replaced words stand out :<font color=blue> words </font>
-Try background colors, and anything else that makes your story look even better. 




ċ
fix.py
(0k)
Samuel Konstantinovich,
Apr 16, 2013, 5:47 AM
Comments