Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port 'HTTP Server' demo to Python #70

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions demos/HTTP Server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const linkbutton = workbench.builder.get_object("linkbutton");
const label_greetings = workbench.builder.get_object("label_greetings");

function handler(_server, msg, _path, _query) {
msg.set_status(200, null);
msg.set_status(Soup.Status.OK, null);
msg
.get_response_headers()
.set_content_type("text/html", { charset: "UTF-8" });
Expand All @@ -24,15 +24,15 @@ function handler(_server, msg, _path, _query) {

function helloHandler(_server, msg, _path, query) {
if (!query) {
msg.set_redirect(302, "/");
msg.set_redirect(Soup.Status.FOUND, "/");
return;
}

const user_agent = msg.get_request_headers().get_one("User-Agent");

label_greetings.label = `Hello ${query.name}, your browser is\n${user_agent}`;

msg.set_status(200, null);
msg.set_status(Soup.Status.OK, null);
msg
.get_response_headers()
.set_content_type("text/html", { charset: "UTF-8" });
Expand Down
80 changes: 80 additions & 0 deletions demos/HTTP Server/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import gi

gi.require_version("Soup", "3.0")
from gi.repository import Soup
import workbench

button_server = workbench.builder.get_object("button_server")
linkbutton = workbench.builder.get_object("linkbutton")
label_greetings = workbench.builder.get_object("label_greetings")

port = 0


def handler(_server, msg, _path, _query):
msg.set_status(Soup.Status.OK, None)
msg.get_response_headers().set_content_type("text/html", {"charset": "UTF-8"})
msg.get_response_body().append(
b"""
<html>
<body>
<form action="/hello">
<label for="name">What is your name?</label>
<input name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
"""
)


def hello_handler(_server, msg, _path, query):
if not query:
msg.set_redirect(Soup.Status.FOUND, "/")
return

user_agent = msg.get_request_headers().get_one("User-Agent")

label_greetings.set_label(f"Hello {query['name']}, your browser is\n{user_agent}")

msg.set_status(Soup.Status.OK, None)
msg.get_response_headers().set_content_type("text/html", {"charset": "UTF-8"})
msg.get_response_body().append(
b"""
<html>
<body>
Thank you, please go back to Workbench.
</body>
</html>
"""
)


def start_server():
global port
server.listen_local(port, Soup.ServerListenOptions(0))
port = server.get_uris()[0].get_port()
linkbutton.set_uri(f"http://localhost:{port}")
button_server.set_label("Stop Server")


def stop_server():
server.disconnect()
linkbutton.set_uri("")
button_server.set_label("Start Server")


def on_clicked(button_server):
if button_server.get_active():
start_server()
else:
stop_server()


server = Soup.Server()

server.add_handler("/", handler)
server.add_handler("/hello", hello_handler)

button_server.connect("clicked", on_clicked)
Loading