It is a memorandum when running the output code on the local web server as "A and pretending to be B" in python.
I referred to the following.
[Mac] I want to make a simple HTTP server that runs CGI with Python
file organization
test/
index.html
cgi-bin/cgi-test.py
index.html
<html>
<head>
<title>Server test</title>
<meta http-equiv="content-type" charset="utf-8">
</head>
<body>
<form action="/cgi-bin/cgi_test.py" method="POST">
<div>
<label for="season">test</label>
<input type="text" name="season" value="test">
<button>Send</button>
</div>
</form>
</body>
</html>
cgi-bin/cgi-test.py
#!/usr/bin/env python
import cgi
import cgitb
import random
cgitb.enable()
print("Content-Type: text/html; charset=utf-8\n\n")
print("<html><body>")
form = cgi.FieldStorage()
#for key in form:
# value = form[key].value
# print('<p>%s: %s</p>' % (key, value))
l = ["Apple",
"Mandarin orange",
"Strawberry",
"pineapple",
"Dragon fruit"]
def bot():
string1=random.choice(l)
string2=random.choice(l)
string=string1+", And pretending to be"+string2
print(string)
bot()
print("</body></html>")
start webserver
$ python3 -m http.server 8080 --cgi
Enter "http://0.0.0.0:8080" in the browser to display index.html If you press the send button and "A and pretending to be B" are displayed, it's OK
[Mac] I want to make a simple HTTP server that runs CGI with Python Python starting from zero 43rd A website using Python can be operated for 100 yen (Part 1) People who only want to write Python tried to make a web application on AWS. AWS Development Diary of Nana Fall Eight Wake Up Everyone's Python Web App-Form Processing
Recommended Posts