Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :>")
share|improve this question

closed as too broad by Joel Cornett, Ferdinand.kraft, chrylis, Mario, Alula Errorpone Aug 23 '13 at 14:43

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
You'll need some sort of webserver, running the html-template, and create a callback function that handles the code. Have a look at some basic cgi/wsgi examples –  dorvak Aug 23 '13 at 12:11

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer

If you want to test it on your local machine, you can create a simple webserver with python. You can find a good tutorial here. You can write python scripts to handle data. Or the other way you should install a real webserver like Apache or NGinx and use cgi or wsgi extensions. The advantage of the second method is that in this case the server can take care of html, css, image, etc files so you can focus on your python code and you don't need to write a whole existing app.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.