Nó lưu trữ các
DOCTYPE html>
Page Title
Sun* Cyber Security Research
0 hoặc các DOCTYPE html>
Page Title
Sun* Cyber Security Research
1 và sử dụng giao thức DOCTYPE html>
Page Title
Sun* Cyber Security Research
2 để gửi các tệp đến trình duyệt webMột số máy chủ web nổi tiếng như. Máy chủ web Nginx, máy chủ web Apache, máy chủ web IIS, máy chủ web Tốc độ ánh sáng
Cách thức máy chủ web hoặc động
Vũ Tiến Hòa @vu. tiền. hoa
Theo dõi
595 22 16
Đã đăng vào ngày 4 tháng 12 năm 2020 9. 21 SA
trong Nhóm An ninh mạng Sun* Hãy hack tất cả mọi thứ
Theo dõi bởi 728 người.
Theo dõi
Sun* Cyber Security Team11. 7k
0
5
Hướng dẫn tạo HTTP Server bằng python- Report
- Add to series of me
Bài đăng này đã không được cập nhật trong 2 năm
Giới thiệu
Web Server là gì?
tổng quan
Máy chủ web là một máy tính lưu trữ những nội dung web
Một máy chủ web được sử dụng để phục vụ các trang web trên mạng internet hoặc mạng nội bộ
Nó lưu trữ các
0 hoặc cácDOCTYPE html> Page Title Sun* Cyber Security Research
1 và sử dụng giao thứcDOCTYPE html> Page Title Sun* Cyber Security Research
2 để gửi các tệp đến trình duyệt webDOCTYPE html> Page Title Sun* Cyber Security Research
Một số máy chủ web nổi tiếng như. Máy chủ web Nginx, máy chủ web Apache, máy chủ web IIS, máy chủ web Tốc độ ánh sáng
Cách thức máy chủ web hoặc động
Ví dụ người dùng muốn xem một trang web như
3, người dùng nhập url vào trình duyệt web với điều kiện người dùng cần kết nối Internet. Khi đó bộ giao thứcDOCTYPE html> Page Title Sun* Cyber Security Research
4 được sử dụng để thiết lập kết nốiDOCTYPE html> Page Title Sun* Cyber Security Research
Khi kết nối được thiết lập, máy khách sẽ gửi yêu cầu thông tin qua
2 và chờ phản hồi từ máy chủ. Phía bên kia máy chủ nhận được yêu cầu, xử lý yêu cầu, gửi lại phản hồi cho máy kháchDOCTYPE html> Page Title Sun* Cyber Security Research
Giao thức HTTP
Giao thức
2 là viết tắt củaDOCTYPE html> Page Title Sun* Cyber Security Research
7DOCTYPE html> Page Title Sun* Cyber Security Research
Nó là một giao thức ở tầng ứng dụng cho phép các ứng dụng web giao tiếp và trao đổi dữ liệu
Nó là một giao thức dựa trên
4DOCTYPE html> Page Title Sun* Cyber Security Research
Nó được sử dụng để cung cấp nội dung.
9DOCTYPE html> Page Title Sun* Cyber Security Research
Sử dụng
2 là cách thuận tiện nhất để chuyển dữ liệu nhanh chóng và đáng tin cậy trên webDOCTYPE html> Page Title Sun* Cyber Security Research
Ví dụ về thông điệp HTTP
Cách tạo HTTP Server bằng python
Python has an số thư viện được tích hợp để tạo một
@import url['//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;
}
1 dễ dàng hơn. Ví dụ bạn có thể tạo một @import url['//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;
}
2 với một lệnh đơn giản- Với python2
python -m SimpleHTTPServer + port[nếu không viết gì mặc định là 8000]
- Với python3
python3 -m http.server + port[nếu không viết gì mặc định là 8000]
Nhưng bạn không thể tùy chỉnh máy chủ của mình
Tạo một dự án HTTPServer
Tạo tệp HTML và CSS
DOCTYPE html>
Page Title
Sun* Cyber Security Research
@import url['//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;
}
Thiết lập dự án
@import url['//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;
}
3from 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 được sử dụng để xử lý các yêu cầu HTTP đến máy chủ
Ngoài ra
4 còn hỗ trợ một số thuộc tính và phương thức sau@import url['//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; }
5. Phương thức này xử lý khi có yêu cầu GET gửi lên@import url['//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; }
6. Phương thức này xử lý khi có yêu cầu POST gửi lên@import url['//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; }
7. Thuộc tính này trả về đường dẫn của yêu cầu@import url['//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; }
8. Phương thức này trả về lỗi HTTP cho khách hàng@import url['//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; }
Đầu tiên định nghĩa một phương thứ
@import url['//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;
}
5. Phương thức này chạy khi có yêu cầu GET gửi lênbản thân. đường dẫn =='/' kiểm tra xem yêu cầu gửi lên có phải trang chỉ mục hay không và nếu là trang
0 thì gán đường dẫn cho chính chỉ mục. đường dẫn == '/chỉ mục. html`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]
Tiếp tục cố gắng đọc các tệp mà người dùng đang cố gắng truy cập ngoại trừ những
1 tránh làm lộ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]
2from 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]
Nếu tệp yêu cầu được tìm thấy, máy chủ sẽ gửi phản hồi
3.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]
3 bị phản hồi mà bất cứ khi nào bạn truy cập thành công một trang webfrom 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]
Nếu yêu cầu tập tin không được tìm thấy, thì
5 gửi mã lỗi tập tin yêu cầu không hợp lệ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]
Sử dụng phương thức mã hóa
6from 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]
7 để chuyển đổi định dạng văn bản sang bytefrom 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
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]
8import 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
sử dụng thời gian để kiếm soát thời gian máy chủ
9 và máy chủ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]
0from http.server import BaseHTTPRequestHandler import os
HTTPServer là một lớp con socketserver. TCPServer nó được tạo và lắng nghe ổ cắm HTTP, gửi yêu cầu đến bộ xử lý
Cấu hình máy chủ với 2 hằng số
from http.server import BaseHTTPRequestHandler
import os
1 với from http.server import BaseHTTPRequestHandler
import os
2 dùng để chạy máy chủ trên localhost và from http.server import BaseHTTPRequestHandler
import os
3 là cổng để chạy ứng dụngfrom http.server import BaseHTTPRequestHandler
import os
4- Gọi HTTPServer mà python cung cấp với đối số thứ nhất là một cặp
5 và đối số thứ 2 là một lớpfrom http.server import BaseHTTPRequestHandler import os
6 để xử lý đã được thiết lập trước đófrom http.server import BaseHTTPRequestHandler import os
try:
httpd.serve_forever[]
except KeyboardInterrupt:
pass
httpd.server_close[]
print[time.asctime[],'Stop Server - %s:%s' %[HOST_NAME,PORT]]
- Khối tiếp theo đến máy chủ
9 và máy chủ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]
0, khi nhận được tín hiệu ngắt từ bàn phím máy chủ sẽ đóng kết nối với máy chủfrom http.server import BaseHTTPRequestHandler import os
9from http.server import BaseHTTPRequestHandler import os
Bây giờ chúng ta khởi động máy chủ bằng cách chạy lệnh
python3 main.py
Open the input url http. //máy chủ cục bộ. 8000
Và đây là phản hồi từ phía máy chủ.
Vì vậy, chúng tôi đã tạo thành công được một Máy chủ HTTP đơn giản với python, ngoài ra chúng tôi còn có thể viết thêm các phương thức như
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]]
0 để Server có thể xử lý nhiều phương thức hơn