Run the output code on the local web server as "A, pretending to be B" in python

Purpose

It is a memorandum when running the output code on the local web server as "A and pretending to be B" in python.

Preparation

I referred to the following.

[Mac] I want to make a simple HTTP server that runs CGI with Python

code

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

test

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

と見せかけて1.png と見せかけて2.png

reference

[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

Run the output code on the local web server as "A, pretending to be B" in python
Test.py is not reflected on the web server in Python3.
Change the standard output destination to a file in Python
Create code that outputs "A and pretending B" in python
I want to be able to run Python in VS Code
Run the Python interpreter in a script
Run Python code on A2019 Community Edition
Create a shortcut to run a Python file in VScode on your terminal
How to install python package in local environment as a general user
Set up a local web server in 30 seconds using python 3's http.server
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
How to input a character string in Python and output it as it is or in the opposite direction.
Run the task in the background on the sshed server
Steps to develop a web application in Python
Output in the form of a python array
How to output "Ketsumaimo" as standard output in Python
Put Docker in Windows Home and run a simple web server with Python
A solution to the problem that the Python version in Conda cannot be changed
I stumbled on the character code when converting CSV to JSON in Python
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
How to run GUI programs such as tkinter in Python environment on WSL2
I searched for the skills needed to become a web engineer in Python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
[Python3] Take a screenshot of a web page on the server and crop it further
Create a plugin to run Python Doctest in Vim (2)
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
Learn Naive Bayes implemented in Python 3.3 on a web page obtained with the Bing API. Let the sentences be classified
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
Run CGI written in python on Sakura's rental server
Write a log-scale histogram on the x-axis in python
Write code to Unit Test a Python web app
[Mac] A super-easy way to execute system commands in Python and output the results
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python
How to run Django on IIS on a Windows server
[Python] How to output the list values in order
How to host web app backend processing in Python using a rental server subdomain
How to set up a simple SMTP server that can be tested locally in Python
I tried "Streamlit" which turns the Python code into a web application as it is
What seems to be a template of the standard input part of the competition pro in python3
How to run Python on Windows without polluting the environment as much as possible (Scoop edition)
"A book to train programming skills to fight in the world" Python code answer example --1.8 "0" matrix
Output "Draw ferns programmatically" to the drawing process in Python
Display "Hello World" created in the local environment on the web
Create a local scope in Python without polluting the namespace
How to use the __call__ method in a Python class
Sort and output the elements in the list as elements and multiples in Python.
Build a Python environment and transfer data to the server
[Introduction to AWS] A memorandum of building a web server on AWS
How to get the last (last) value in a list in Python
Save images on the web to Drive with Python (Colab)
Flask Web server cannot be published to the outside [VScode]
I tried to build an environment where work in the Docker container on the remote server can be done directly from the local VS Code with SSH connection
"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
"A book to train programming skills to fight in the world" Python code solution example --1.5 One-shot conversion
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
How to easily create an environment where python code runs on Jupyter without polluting the local environment
"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks
"A book to train programming skills to fight in the world" Python code Solution example --1.7 Matrix rotation
"A book to train programming skills to fight in the world" Python code Solution example --2.8 Loop detection