python :- passing HTML form input to python code and execute it

标签 python html forms cgi

<分区>

您好,我想从 html 表单获取输入并将其传递给我的 python 脚本并使其执行,然后我想在不使用任何框架的情况下在浏览器中打印我的结果。下面是我的 python 代码:

import re

hap=['amused','beaming','blissful','blithe','cheerful','cheery','delighted']

sad=['upset','out','sorry','not in mood','down']

sad_count=0

happy_count=0

str1=raw_input("Enter Message...\n")

happy_count=len(filter(lambda x:x in str1,hap)) 

sad_count=len(filter(lambda x:x in str1,sad))

if(happy_count>sad_count):

        print("Hey buddy...your mood is HAPPY :-)")

elif(sad_count>happy_count):

            print("Ouch! Your Mood is Sad :-(")

elif(happy_count==sad_count):

        if(happy_count>0 and sad_count>0):

            print("oops! You are in CONFUSED mood :o")

        else:
            print("Sorry,No mood found :>")

最佳答案

看起来你使用的是 python3,但在 python 2.7 中使用 BaseHTTPServer(即 python3 中的 HTTP.server)你可以做类似的事情

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write("""
            <html><head></head>
            <body>
            <form method="POST">
            your mood:
            <textarea name="mood">
            </textarea>
            <input type="submit" name="submit" value="submit">
            </form>
            </body>
            </html>
            """)
        return

    def do_POST(self):
        form = cgi.FieldStorage(
            fp=self.rfile, 
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })
        themood = form["mood"]
        hap=['amused','beaming','blissful','blithe','cheerful','cheery','delighted']
        sad=['upset','out','sorry','not in mood','down']
        sad_count=0
        happy_count=0
        happy_count=len(filter(lambda x:x in themood.value,hap)) 
        sad_count=len(filter(lambda x:x in themood.value,sad))
        if(happy_count>sad_count):
            self.wfile.write("Hey buddy...your mood is HAPPY :-)")
        elif(sad_count>happy_count):
            self.wfile.write("Ouch! Your Mood is Sad :-(")
        elif(happy_count==sad_count):
            if(happy_count>0 and sad_count>0):
                self.wfile.write("oops! You are in CONFUSED mood :o")
            else:
                self.wfile.write("Sorry,No mood found :>")
        return
server = HTTPServer(('', 8181), Handler)
server.serve_forever()

希望对你有帮助

关于 python :- passing HTML form input to python code and execute it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402152/

相关文章:

user-interface - Netbeans 表单加载问题

Flutter DropdownButtonFormField 正在扩展但不应该扩展

python - matplotlib:在时间序列图中将周期标签置于周期数据的中心

python - 在 Mac 上的 Python 2.7.3 中导入 .pyd(使用 SWIG 创建)

html - 浏览器中的奇数/不规则 float CSS 元素

jquery - 使用 JQuery 将相同的计算应用于不同的输入

javascript - 在 Node-js 中处理 HTML 事件

python - 尝试将文件从一个目录复制到另一个 w/paramiko 时出现 Windows 错误

python - VS Code 无法打开 ipynb 文件

html - 为什么我的 html 表单的密码输入字段有浅蓝色背景?