Reputation: 93
#!/usr/bin/python
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Python</p>";
#this is my very first python attempt
name=raw_input('Enter your name : ')
print ("Hi %s, Let us be friends!" % name);
print "</body></html>";
So, This is my entire code. This is the very first time I am using python and don't know if I configured it correctly. I simply want a user to input their name, but I am getting no area for them to type their input.
http://rollinsfamilytree.com/scgi-bin/testerpy.py
I simply get the words "Enter your name :" and then nothing. I can't enter a name. Please tell me what I am doing wrong. Do I not need to have it running in HTML to run the full script? (I'm sorry if that is such a dumb question - again, I litterlly just starting learning yesterday)
Upvotes: 2
Views: 2244
Reputation: 378
From your post and your comments, I understand you want to have a form in which to input your name, submit it and then have "Hi , Let us be friends!" printed. If that is the case, you need to add "forms" in your code which currently you don't have. I would suggest that you check this tutorial for a good explanation on the topic:
Learn Python The Hard Way: Getting Input From A Browser
Of course, you will need to first submit your input(this would require a refresh of the web-page) before you get the desired output. You could also look in dynamic web programming to avoid the need of webpage reload.
Upvotes: 4
Reputation: 159905
The trick here is that raw_input
waits for input from Standard In (abbreviated stdin
) - however, you don't have an interactive stdin
in CGI (if I recall correctly, stdin
is the request).
If you want to get input from the user over HTTP you will need to use a HTML form
and get the data out when the user responds:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
name = form.getvalue('name')
# Note the use of triple-quoted strings
# and `.format` rather than `%`.
# Much easier to do multi-line text this way.
print """Content-type: text/html
<html><head>
<title>CGI Test</title>
</head><body>
<p>Test page using Python</p>
<form>
<label>What is your name?
<input type="text" name="name" value="{provided_name}"></label>
</form>
Hi {provided_name}, Let us be friends!
</body></html>""".format(provided_name=name)
However, you will probably be better off with a lightweight framework that abstracts all of this away from you like Bottle or Flask.
Upvotes: 5
Reputation: 34337
CGI (common gateway interface) displays text! The stdin or terminal on the server is not opened on your browser or available via some kind of proxy.
If you want to get input and display it with CGI you have to use a two step approach
1) display a html page with a <form>
tag that includes a <input type="text">
and a submit button.
2) Once the form is filled in and submit pressed the input text goes back to the server and you can use it to make a second page saying "Hi %s, Let us be friends!"
If you want to do something more interactive then you'll have to use javascript and css. The Dojo toolkit is quite easy to use
Upvotes: 1
Reputation: 15028
It is not as simple as you think. The web browser is not a terminal, so you cannot treat it as one. If you want to use Python on the web, you have a lot to learn; things I think you learn best by doing. Try looking into Bottle or Django. Or any of the other web frameworks for Python.
Upvotes: 1
Reputation: 369094
Change your code as follow:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
name = form.getvalue('name')
print "Content-type: text/html\n\n"
print "<html><head>"
print "<title>CGI Test</title>"
print "</head><body>"
print "<p>Test page using Python</p>"
print "Hi %s, Let us be friends!" % name
print "</body></html>"
Then access http://rollinsfamilytree.com/scgi-bin/testerpy.py?name=nobody . (Don't omit trailing ?name=nobody)
Upvotes: 2