Pregunta

http://www.tutorialspoint.com/python/python_cgi_programming.htm

#!/usr/bin/python

 print "Content-type:text/html\r\n\r\n"
 print '<html>'
 print '<head>'
 print '<title>Hello Word - First CGI Program</title>'
 print '</head>'
 print '<body>'
 print '<h2>Hello Word! This is my first CGI program</h2>'
 print '</body>'
 print '</html>'

What is the significance of #!/usr/bin/python

It says to save file as hello.py and save it to /var/www/cgi-bin directory but I don't have this directory, should I create one?

Before running your CGI program, make sure you have change mode of file using chmod 755 hello.py UNIX command to make file executable.

but where to (how to) execute this chmod command? should I include $chmod 755 "/location of hello(.py).../" in hello.py?

¿Fue útil?

Solución

You have to include the #!/usr/bin/python line on *nix systems to tell the shell how to interpret this script and where your interpreter is in the system. This is called the shebang line.

The chmod 755 hello.py command is a unix shell command to change the permissionson of the file and make it executable. Doing this you can run the script without having to do python hello.py but instead ./hello.py .

From my understanding you are a windows user. In this case, you don't have to do all of this. Just save the file as hello.py and that's it.

This post on tutorialspoint.com implies that you already have an HTTP server running that supports cgi and has a default web directory of /var/www/

Otros consejos

The line #!/usr/bin/python is a signal to the shell (the program that handles your keyboard input and runs commands for you) that this file is a script that can be handled by the program shown. It's called a 'shebang' or sometime 'hashbang'.

In this case the shebang tells your shel to run the program it finds at /usr/bin/python and submit this script to it. There's more here

chmod is a general command for setting permissions on files. You execute it from the command line like any other command. The numerical values for the permissions can be a bit cryptic, but they're made up of binary bits that indicate certain permissions. Changing a permission to 755 specifically indicates that the owner of the file can execute this as a program, rather than just treating it as a block of text. Personally I prefer the mor descriptive chmod u+x filename - add execute for the user.

You can find a fuller description of chmod here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top