Python: - 将HTML表单输入传递给python代码并执行它 [英] Python :- passing HTML form input to python code and execute it

查看:243
本文介绍了Python: - 将HTML表单输入传递给python代码并执行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import re 

hap = ['amused','beaming' ,'幸福','愉快','快乐','愉快','高兴']

悲伤= ['心烦','失去','抱歉' 'down']

sad_count = 0

happy_count = 0

str1 = raw_input(Enter Message ... \\\


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

sad_count = len(过滤器(lambda x:str1中的x,sad))

if(happy_count> sad_count):

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

elif(sad_count> happy_count ):

print(哎哟!你的心情很难过:-()

elif(happy_count == sad_count):

if happy_count> 0 and sad_count> 0):

print(oops!你处于CONFUSED心情:o)

其他:
print(Sorry,没有找到心情:>)


解决方案

ems这是你使用python3,但是在Python 2.7中使用BaseHTTPServer(即python3中的HTTP.server),你可以做一些这样的事情

  BaseHTTPServer导入BaseHTTPRequestHandler,HTTPServer 
导入cgi

类Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(
< html>< head>< / head>
< body>
< form method =POST>
您的心情:
< textarea name =mood>
< / textarea>
< input type =submitname =submitvalue =submit>
< / form>
< / body>
< / html>

返回

def do_POST(self):
form = cgi.FieldStorage(
fp = self.rfile,
headers = self.headers,
environ = {'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers ['Content-Type'],
})
boodood = form [mood]
hap = ['amused','beaming','blissful','blithe','cheerful','cheery','dighted']
sad = ['upset','out','sorry','not in mood','down']
sad_count = 0
happy_count = 0
happy_count = len(filter(lambda x: (b)如果(happy_count> sad_count):
self.wfile。
sad_count = len(filter(lambda x:x in theseood.value,sad))
。写(嘿伙计......你的心情很开心:-))
elif(sad_count> happy_count):
self.wfile.write(哎哟!你的心情很悲伤: - ()
elif(happy_count == sad_count):
if(happy_count> 0 and sad_count> 0):
self.wfile.write(oops! (对不起,找不到心情:>)
返回
服务器= HTTPServer((()($())
else:
self.wfile.write '',8181),Handler)
server.serve_forever()

我希望能帮助你

Hi i want to get input from html form and pass it to my python script and make it execute and then i want to print my result in browser without using any framework. below is my python code :

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 :>")

解决方案

It seems that's you use python3 but in python 2.7 with the BaseHTTPServer (that is HTTP.server in python3) you can do some thing like that

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()

i hope that can help you

这篇关于Python: - 将HTML表单输入传递给python代码并执行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆