Download an Image Using Python Client From Express.js Server
The objective of this article is to create a python client which will send an HTTP request to the server that is running on express.js framework for downloading an image.

The code for this project is also available in my GitHub repository -

Developing the server

We’ll develop the back-end of this application in Node.js using Express.js framework. For that, let’s create a directory for our server application with the name myServer, which will contain the following files and folders.

We’ll open the terminal and navigate into this directory. We’ll then create a folder with name public and inside that we’ll keep any image with name image.jpg.

After that we’ll run the command npm init and we’ll choose all the default options that will pop up. Then we’ll install express using the following command.

npm install express --save

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Now we’ll write the code for our server application in the file app.js.

When we run the above code, it will make our server operational and will listen to calls on port 3000. And when somebody tries to access the /image route, the server will send image.jpg file to the client.

Developing the client

Now, we’ll write code for our client application in python.

This application will send an HTTP request to the server and will receive the image as a byte string, which will be decoded and stored in the local system.

Now that we have created our client as well as server application, it’s the time to test it.

Testing the application

First of all, we’ll run the server application in the terminal using the command-

node app.js

It will give the output as

server is listening on port: 3000

Then, we’ll open another terminal window and navigate to the directory where the client.py file is located. Then, we’ll run the command —

python client.py

It will give the following output in the terminal

HTTP/1.1 200 OK

X-Powered-By: Express

Accept-Ranges: bytes

Cache-Control: public, max-age=0

Last-Modified: Mon, 10 Oct 2016 11:18:54 GMT

ETag: W/"290453-157ea522f31"

Content-Type: image/jpeg

Content-Length: 2687827

Date: Tue, 24 Nov 2020 20:23:20 GMT

Connection: close

and will download the image and store it with the name downloaded_image.jpg in the same directory where client.py is located.

References
['Souvik Paul']
['Python3', 'Nodejs', 'Servers', 'Expressjs', 'Clients']