[Tutor] python cgi single double quotes
Peter Otten
__peter__ at web.de
Wed Jul 20 05:45:04 EDT 2016
nitin chandra wrote:
> On inserting the line ...
>
> print "<p> + "sys.version + "</p>"
>
> required slight correction
>
> print "<p>" + sys.version + "</p>"
>
> and the following script and its output are below :-
> ----------------------------------------
> #!/usr/bin/env python
>
> import sys
> import cgi
> import psycopg2
>
> 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 "<p>"+ sys.version + "</p>"
> print 'First name: <input type="text" name="fname"><br>'
> print '</body>'
> print '</html>'
>
> ---------------------------------------------------
>
> Hello Word! This is my first CGI program
>
> 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]
>
> First name:
If you got that from your server like in the session below...
$ cat first.py
#!/usr/bin/env python
import sys
import cgi
import psycopg2
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 "<p>"+ sys.version + "</p>"
print 'First name: <input type="text" name="fname"><br>'
print '</body>'
print '</html>'
$ curl http://myhost/somewhere/first.py
<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Hello Word! This is my first CGI program</h2>
<p>2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2]</p>
First name: <input type="text" name="fname"><br>
</body>
</html>
... the following should also work:
$ cat second.py
#!/usr/bin/env python
import sys
import cgi
import psycopg2
print """Content-type:text/html\r\n\r\n
<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Hello Word! This is my first CGI program</h2>
<p>{version}</p>
First name: <input type="text" name="fname"><br>
</body>
</html>
""".format(version=sys.version)
$ curl http://myhost/somewhere/second.py
<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Hello Word! This is my first CGI program</h2>
<p>2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2]</p>
First name: <input type="text" name="fname"><br>
</body>
</html>
If it does you can start looking for the actual problem. I don't expect it
to have anything to do with your choice of quoting characters, as long as
you write legal Python 2.
More information about the Tutor
mailing list