- PYTHON 3 DEMOPACK DOWNLOAD
- GeeXLab Downloads
- Forum thread (EN)
Let’s look at one of the important modules that is included in the Python standard library: the socket module.
This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms.
The reference guide of the socket module is available here: socket — Low-level networking interface.
I prepared a demo that shows a simple use case of the socket module: how to send an HTTP GET request to get the content of a webpage.
The demo requires GeeXLab 0.27.2+. Download the demopack, unzip it where you want and load into GeeXLab (drag an drop) the socket/01-socket-http-request/main.xml file.
By default the target is www.google.com. But you can change it to any other target. Then click on the Send HTTP GET request - Port 80 button. This operation will send the following request to google.com:
GET / HTTP/1.1\r\nHost:www.google.com\r\n\r\n
And you will receive the following reply from Google server:
Here is the Python 3 code that executes the HTTP GET request (line 84 in the demo frame.py file):
target_host = "www.google.com" target_port = 80 # create a socket object client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect the client client.connect((target_host,target_port)) # send some data request = "GET / HTTP/1.1\r\nHost:%s\r\n\r\n" % target_host client.send(request.encode()) # receive some data response = client.recv(4096) http_response = repr(response) http_response_len = len(http_response) #display the response gh_imgui.text("[RECV] - length: %d" % http_response_len) gh_imgui.text_wrapped(http_response)
In the demo, the target_host variable has been filled up by the input text (ImGui function).