It is very simple to create a client and server application in Python.Infact you don’t need any web server to create a simple client server application in Python.Python provides several modules for accessing the network using different protocols.
The urllib package contains the modules for accessing the network resources.
For example to create a HTTP Server you can use the following example:
from http.server import HTTPServer, SimpleHTTPRequestHandler httpserver = HTTPServer(("", 8000), SimpleHTTPRequestHandler) httpserver .serve_forever()
The BaseHTTPRequestHandler is a module which is used for creating webserver.It is implemented by derived classes.Two such classes are SimpleHTTPServer and CGIHTTPServer modules.In this example we are using the SimpleHTTPRequestHandler for serving the contents of a file.
To create a client which calls the above webserver to access the file contents you can use the following sample:
from urllib.request import urlopen file = urlopen("http://localhost:8000") print(file.readlines())
Leave a Reply