0

I have wrote a website using html/css and javascript and I am running this site using a simple python script

import http.server
import socketserver

PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

the website is running just fine. However I am trying to understand how can I send data from the js client to the python server? Let's settle a simple example. Let's assume that I have a simple html text input and a simple html button and I am writing some text to the field, how am I supposed to send this text, when I am pressing the button, to the server?

NOTE: I am using pure javascript so far

3
  • Possible duplicate of Ajax tutorial for post and get Commented Jul 24, 2019 at 11:49
  • have you checked the fetch api or axios Commented Jul 24, 2019 at 11:50
  • @YegorZaremba I am trying to used pure javascript as you can read on the NOTE section. I am asking for the python script as well Commented Jul 24, 2019 at 12:03

1 Answer 1

1

You should implement in Python a handler that can handle a POST or a PUT request onto a given URL. And in javascript, you just send your HTTP request on this URL with the correct HTTP method and with data in the body of your request.

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

3 Comments

You should have a look at a simple Python library for building your Web server. For example Flask: palletsprojects.com/p/flask
I was not aware of flask I made a search online about it and I found the example I was asking for https://www.tutorialspoint.com/flask/flask_http_methods.htm
Glad to be helpful. :-)

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.