Elizabeth
Elizabeth

Reputation: 282

How to use HTTP server for getting input from the client and manipulate it

I'v been using python before but never web.
I want to write a python program for generating a server that get the client input and do some function with it.
for example: a word.
And than return it's ASCII value.
I guess it's a very basic question, but I am totaly new to this stuff.

Upvotes: 1

Views: 3445

Answers (2)

scope
scope

Reputation: 1997

Easy way is to use BaseHTTPServer, check this example:

import BaseHTTPServer

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  def do_GET(self):
    requestPath = self.path
    self.send_response(200)
    self.send_header('Content-type', 'application/json')
    self.end_headers()
    self.wfile.write("requested path: " + requestPath)


httpServer = BaseHTTPServer.HTTPServer(('127.0.0.1', 80), RequestHandler)
httpServer.serve_forever()

Note: To run on port 80 you need permissions, so either run this with sudo or use some bigger port number (like 8080).

Upvotes: 1

Robert Townley
Robert Townley

Reputation: 3574

For local development, you can play around with running a SimpleHTTPServer to receive HTTP requests: https://docs.python.org/2/library/simplehttpserver.html

If you want to make a full-on web service or API that can take in input and respond to it, that sounds like a great use case for one of the Python web frameworks. I'm partial to Django, but Flask might be a bit easier to get set up with.

Upvotes: 0

Related Questions