DelphiLearn PythonPythonPython GUIRAD Studio

Easily Create A Powerful Python Web Server In A Delphi Windows GUI App

blogbanner3 11

Python Web Server can be set up in two ways. Python supports a Web Server in the “out of the box” way. We can even start a web server with just a one-liner in Python!

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Python Web Server in Python GUI apps and gets the output. The web server in this example can be accessed on your local network only. 

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1
Open Demo01dproj

 

1. One-Liner Example

Let’s run the built-in web server in our command prompt, using this command:

That will open a web server on port 8000. You can then open your browser at http://127.0.0.1:8000/ or http://localhost:8000/

The web server is also accessible over the network using your 192.168.-.- address. This is a default server that you can use to download files from the machine.

Let’s see it in action, both in your command prompt and browser:

1 1 9807135
Python Web Server One Liner Example
1 2 4832172
Python Web Server One Liner Example

 

2. Web Server in Python GUI in Python4Delphi

Let’s run the code below in our Demo01 VCL to start a custom web server. To create a custom web server, we need to use the HTTP protocol. By design the http protocol has a “get” request which returns a file on the server. If the file is found it will return 200.

The server will start at port 8080 and accept default web browser requests:

Output in our Python GUI:

2 1 2150463
Web Server in Python GUI in Python4Delphi

If you open the URL like http://127.0.0.1:8000/ or http://localhost:8000/ the method do_GET() is called. We send the web page manually in this method.

Appearance in our browser:

2 2 7276165
Web Server in Python GUI in Python4Delphi

The variable self.path returns the web browser URL requested.

However, http.server is not recommended for production. It only implements basic security checks.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *