翼度科技»论坛 编程开发 python 查看内容

基于Python创建可定制的HTTP服务器

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
要编写一个简单的能发布网页的 Python 应用服务器,可以使用 Python 自带的
  1. http.server
复制代码
模块来实现。
  1. http.server
复制代码
模块是 Python 的标准库,可以用来快速搭建一个简单的 Web 服务器。
以下是一个简单的示例,演示如何使用
  1. http.server
复制代码
模块来实现一个能够发布网页的应用服务器:
步骤如下:

1、编写服务端代码

命名为httpserver.py文件
  1. import http.server
  2. import socketserver

  3. PORT = 8080

  4. Handler = http.server.SimpleHTTPRequestHandler

  5. with socketserver.TCPServer(("", PORT), Handler) as httpd:
  6.     print(f"Serving at port {PORT}")
  7.     httpd.serve_forever()
复制代码
这个应用服务器将会在本地的 8080 端口监听来自客户端的 HTTP 请求,并将当前目录下的网页文件(如 HTML、CSS、JS 等)发布给客户端。
要使用这个应用服务器,只需要将网页文件放在当前目录下,然后在终端中运行上述 Python 脚本即可。在浏览器中访问 http://localhost:8080 即可访问网页。

2、编写网页html+css文件

命名为index.html和style.css文件
index.html:
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.           <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>登录页面</title>
  6.     <link rel="stylesheet" type="text/css" href="style.css">
  7.   </head>
  8.   <body>
  9.     <div class="container">
  10.       <form>
  11.         <h2>欢迎登录</h2>
  12.         <label for="username"><b>用户名</b></label>
  13.         <input type="text" placeholder="请输入用户名" name="username" required>
  14.         <label for="password"><b>密码</b></label>
  15.         <input type="password" placeholder="请输入密码" name="password" required>
  16.         <button type="submit">登录</button>
  17.       </form>
  18.     </div>
  19.   </body>
  20. </html>
复制代码
style.css:
  1. body {
  2.   background-color: #F8F8F8;
  3.   font-family: Arial, sans-serif;
  4. }
  5. .container {
  6.   width: 400px;
  7.   margin: 0 auto;
  8.   margin-top: 50px;
  9.   background-color: #FFFFFF;
  10.   padding: 20px;
  11.   border-radius: 10px;
  12.   box-shadow: 0px 0px 10px #888888;
  13. }
  14. form {
  15.   display: flex;
  16.   flex-direction: column;
  17. }
  18. h2 {
  19.   text-align: center;
  20.   margin-bottom: 20px;
  21. }
  22. label {
  23.   font-size: 18px;
  24.   margin-bottom: 10px;
  25. }
  26. input[type="text"],
  27. input[type="password"] {
  28.   padding: 10px;
  29.   margin-bottom: 20px;
  30.   border: none;
  31.   border-radius: 5px;
  32.   box-shadow: 0px 0px 5px #888888;
  33. }
  34. button[type="submit"] {
  35.   background-color: #4CAF50;
  36.   color: #FFFFFF;
  37.   font-size: 16px;
  38.   font-weight: bold;
  39.   padding: 10px;
  40.   margin-top: 20px;
  41.   border: none;
  42.   border-radius: 5px;
  43.   cursor: pointer;
  44. }
  45. button[type="submit"]:hover {
  46.   background-color: #3E8E41;
  47. }
复制代码
3、复制html+css到服务端py文件同一文件夹下



4、运行服务端程序



5、浏览器中输入localhost:8080

显示如下:

程序达到预期目标。
到此这篇关于基于Python创建可定制的HTTP服务器的文章就介绍到这了,更多相关Python定制HTTP服务器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/article/283920.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具