Web Services
Web Services
Rinav (2022BCSE005)
What are web-services?
The client makes a request, server processes it and sends back a response.
The “web service” in the server receives a request from the client and sends back a
response. The address where the client can access the web service is called a URL.
HTTP (Hypertext Transfer Protocol): The primary protocol for web communication, sending
requests (like GET for data retrieval or POST for data submission).
HTTPS: An encrypted version of HTTP. It secures data sent between the client and server,
which is essential for sensitive information
Building web-service using python
Python has a built-in module, http.server, that allows us to set up a basic web server
without external dependencies.
We just need to inherit the BaseHTTPRequestHandler class using our custom class
and override some of the methods to create a minimal web-server
To receive any ‘GET’ requests on the server we have to override the do_GET method
send_response(200) sends HTTP response with status code 200 which means “Ok” or request successful.
send_header(...) adds headers to response containing the metadata about response, it is optional and is
auto-generated unless response is super complicated.
wfile.write() writes the data provided to it to the output stream which in this case is current serving stream.
Building web-service using python
To receive any ‘POST’ requests on the server we have to override the do_POST method
self.headers is just headers of the POST request, we are acquiring the length of request through headers.
rfile.read() reads the data provided in input stream which in this case is from the request to the server. It
takes length of data to be read as argument.
We can simply run this server using HTTPServer class object which takes address and
request handler class as arguments.
Both POST and GET requests are working fine and we are getting relevant responses too.
Easier, Go-to method
We can simply run a server using python command line utilities too.
This will start serving from the current directory and look for a entry point like index.html
or any other module that can be run on web or can be served and start it.
You can also browser directories in browser and then choose the module.
THANK YOU