jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

Python2-3.x版本创建一个http服务脚本 #44

Open jackieli123723 opened 6 years ago

jackieli123723 commented 6 years ago

Python2-3.x版本创建一个http服务脚本可使用于spa或者构建文件下载系统

两种方式 一个是写入package.json 一个是创建一个py脚本

说明build 是放html资源的入口 例如spa中打包的dist同理

npm node命令方式(前提是本机安装了python)

// Python3请看
// python -m http.server 8989
//
// Python2请看
// python -m SimpleHTTPServer 8989

//python -m http.server 8989  || python -m SimpleHTTPServer 8989
"startp2": "python -m SimpleHTTPServer 8989",
 "serverp3": "python -m http.server 8989",

server.py 两种方式

#way1 http://localhost:8888/build/
import SimpleHTTPServer
import SocketServer

PORT = 8888

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

# way2 -- http://localhost:8000/build/
# python server.py

# import sys
# import BaseHTTPServer
# from SimpleHTTPServer import SimpleHTTPRequestHandler
# HandlerClass = SimpleHTTPRequestHandler
# ServerClass = BaseHTTPServer.HTTPServer
# Protocol = "HTTP/1.0"
#
# if sys.argv[1:]:
#   port = int(sys.argv[1])
# else:
#   port = 8000
# server_address = ('127.0.0.1', port)
#
# HandlerClass.protocol_version = Protocol
# httpd = ServerClass(server_address, HandlerClass)
#
# sa = httpd.socket.getsockname()
# print "Serving HTTP on", sa[0], "port", sa[1], "..."
# httpd.serve_forever()

pm20启动

$ pm2 start server.py --name python-spa-server
[PM2] Done.
┌───────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name          │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ watching │
├───────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ python-spa-server │ 3  │ fork │ 16748 │ online │ 0       │ 0s     │ 12% │ 8.9 MB    │ disabled │
│ server            │ 0  │ fork │ 11632 │ online │ 0       │ 24h    │ 0%  │ 46.8 MB   │ disabled │
│ server            │ 1  │ fork │ 11816 │ online │ 0       │ 18h    │ 0%  │ 29.2 MB   │ disabled │
└───────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
pm2 start upload.py --interpreter="python"
[PM2] Done.
┌───────────────────┬──────┬────────┬───
┬─────┬───────────┐
│ Name              │ mode │ status │ ↺ │ cpu │ memory    │
├───────────────────┼──────┼────────┼───
┼─────┼───────────┤
│ python-spa-server │ fork │ online │ 0 │ 0%  │ 1.0 MB    │