0% found this document useful (0 votes)
3 views9 pages

Web Services

Web services enable applications to communicate over the internet using standard protocols like HTTP and HTTPS. Python can be used to build web services by utilizing the http.server module, allowing for the handling of GET and POST requests through custom request handlers. The server can be tested using tools like 'curl' and can also be run using Python command line utilities.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Web Services

Web services enable applications to communicate over the internet using standard protocols like HTTP and HTTPS. Python can be used to build web services by utilizing the http.server module, allowing for the handling of GET and POST requests through custom request handlers. The server can be tested using tools like 'curl' and can also be run using Python command line utilities.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

WEB-SERVICES

Rinav (2022BCSE005)
What are web-services?

Web services allow applications to communicate over the internet using


standard protocols.

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

First we import the required exports


from http.server module and then
inherit the base class
‘BaseHTTPRequestHandler’ using
our custom ‘requestHandler’
Building web-service using python

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.

end_headers() ends the header

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.

At end we’re returning back same data we received.


Building web-service using python

We can simply run this server using HTTPServer class object which takes address and
request handler class as arguments.

Server address has to be a tuple containing address and port.

HTTPServer.serve_forever() starts the server.

I am taking port as a user input


Testing the service

We can easily test the server using ‘curl’...

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy