pages, scripts, progarms
or files multimedia
and use the protocol HTTP
to send the file to the web browser.www.google.com
, the user enters the url into the web browser provided the user needs an Internet connection. The protocol set TCP/IP
is then used to establish the connection.HTTP
and waits for a response from the server. On the other side, the server receives the request, processes the request, and sends the response back to the client.HTTP
stands for Hyper Text Transfer Protocol
.TCP/IP
.hình ảnh, âm thanh, video, tài liệu
…HTTP
is the most convenient way to move data quickly and reliably on the web.Python has a number of built-in libraries to make web server
it easier. For example, you can create one SimpleHTTPServer
with a simple command:
python -m SimpleHTTPServer + port (if not write anything default is 8000)
python3 -m http.server + port (if not write anything default is 8000)
But you cannot customize your server.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/main.css">
</head>
<body>
<h1>Sun* Cyber Security Research</h1>
</body>
</html>
main.css
@import url('https://fonts.googleapis.com/css?family=IBM+Plex+Sans+Condensed:100,200,300,400');
body{
background-color: #222;
}
h1{
color: white;
padding: 20px 40px;
margin: 0 auto;
background-color: rgba(255, 255, 255, .7);
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
border-radius: 4px;
border-bottom: 5px solid rgba(150, 150, 150, 1);
font-weight: 200;
font-family: 'IBM Plex Sans Condensed', sans-serif;
}
server.py
from http.server import BaseHTTPRequestHandler
import os
class Server(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
try:
split_path = os.path.splitext(self.path)
request_extension = split_path[1]
if request_extension != ".py":
f = open(self.path[1:]).read()
self.send_response(200)
self.end_headers()
self.wfile.write(bytes(f, 'utf-8'))
else:
f = "File not found"
self.send_error(404,f)
except:
f = "File not found"
self.send_error(404,f)
from http.server import BaseHTTPRequestHandler
import os
BaseHTTPRequestHandler
supports some of the following properties and methods:do_GET()
: This method deals with GET requests.do_POST()
: This method deals with POST requests.path
: This property returns the path of the request.send_error()
: This method returns an HTTP error to the client.First define a means do_GET()
. This method runs when there is a GET request sent.
index
then assign the path to the index self.path == '/ index.html` .file python
avoid leaks source code
.200
. 200
is the response that whenever you visit a website successfully.server
an invalid request file error code is sent.bytes()
encode method utf-8
to convert text to bytes.main.py
import time
from http.server import HTTPServer
from server import Server
HOST_NAME = 'localhost'
PORT = 8000
if __name__ == "__main__":
httpd = HTTPServer((HOST_NAME,PORT),Server)
print(time.asctime(), "Start Server - %s:%s"%(HOST_NAME,PORT))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
import time
from http.server import HTTPServer
from server import Server
start
and stop
server.Configure the server with two constants HOST_NAME = 'localhost',PORT = 8000
with which HOST_NAME = 'localhost'
to run the server on localhost and which PORT = 8000
is the port where the application will be run.
httpd = HTTPServer((HOST_NAME,PORT),Server)
(HOST_NAME,PORT)
and the second argument as a class Server
to handle that was set earlier.try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(),'Stop Server - %s:%s' %(HOST_NAME,PORT))
start
and the stop
server, when receiving an interrupt signal from the server keyboard will close the connection httpd.server_close()
.Now we start the server by running the command:
python3 main.py
So we have successfully created a simple HTTP Server with python, in addition we can write more methods such as the HEAD, POST ...
Server can handle more methods.
#python #programming