2

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.

1
  • This isn't really a SO type question, but i'd check out Django or Flask if I was you. Commented Nov 14, 2017 at 23:12

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used it and what I got when I typed: localhost:8000 on the browser was: requested path:/ And back to my question: I want that the user could write things in it, I will run some function on the input and return the output. How can I do it?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.