Tags:
tag this topic
create new tag
view all tags
---+ Writing a WEB server from scratch We do not want to re-invent the wheel, and we will use a WEB server framework already prepared for us by professional programmers. It is nevertheless interesting to implement a very primitive WEB server ourselves, simply to understand the basic principles. ---++ The Hello World WEB Server Let's start simple and produce a WEB server that just sends "Hello World!" to the browser when called. You may add some HTML text if desired. In the first version we will write a TCP server creating a stream socket and binding to port 80. <br /> <br /> <verbatim>s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: s.bind(('',80)) except OSError as exception: if exception.args[0] = uerrno.EADDRINUSE: print("Socket is already bound, please reset the machine") sys.exit()</verbatim> The server then starts listening for an incoming connection request and establishes a connection by accepting it. <verbatim>s.listen(5) print("Starting the Hello World WEB server on IP address ",ipaddr,"port 80") while True: conn,addr=s.accept() print("GOT a connection from %s" % str(addr))</verbatim> Finally, it reads an HTTP request from the connection and answers with its Hello World page: <verbatim>request=conn.recv(1024) print("Content %s" % str(request)) request=str(request) response=html conn.send(response) conn.close()</verbatim> _HTML_ is the HTTP string to be sent, e.g. <verbatim>html = """ <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <title>Hello World </title> <h1>The Hello World! HTML page</h1> <p>Hello World!</p> </body> </html> """</verbatim> Please check <a href="https://docs.micropython.org/en/latest/library/usocket.html" target="_blank">https://docs.micropython.org/en/latest/library/usocket.html <img alt="" border="0" height="12" src="%PUBURL%/TWiki/TWikiDocGraphics/external-link.gif" width="13" /></a>for more details. Here is the complete program: %ATTACHURL%/helloWorldWebServerV1.py.txt -- %USERSIG{UliRaich - 2020-06-08}% ---++ Comments %COMMENT%
Attachments
Attachments
Topic attachments
I
Attachment
History
Action
Size
Date
Who
Comment
txt
helloWorldWebServerV1.py.txt
r1
manage
1.7 K
2020-06-08 - 12:06
UliRaich
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r3 - 2022-11-12
-
UliRaich
Home
Site map
AFNOG web
Embedded_Systems web
IoT_Course_English web
IoT_Course_French web
Main web
Sandbox web
TWiki web
IoT_Course_English Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
E
dit
A
ttach
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback