一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python3 如何開啟自帶http服務(wù)

Python3 如何開啟自帶http服務(wù)

2021-11-04 10:09HanZhizhi Python

這篇文章主要介紹了Python3 開啟自帶http服務(wù)的操作方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

開啟Web服務(wù)

1.基本方式

Python中自帶了簡單的服務(wù)器程序,能較容易地打開服務(wù)。

在python3中將原來的SimpleHTTPServer命令改為了http.server,使用方法如下:

1. cd www目錄

2. python -m http.server

開啟成功,則會輸出“Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …”,表示在本機8000端口開啟了服務(wù)。

如果需要后臺運行,可在命令后加"&"符號,Ctrl+C不會關(guān)閉服務(wù),如下:

?
1
python -m http.server &

如果要保持服務(wù),則在命令前加nohup以忽略所有掛斷信號,如下:

?
1
nohup python -m http.server 8001

2.指定端口

如果不使用默認端口,可在開啟時附帶端口參數(shù),如:

?
1
python -m http.server 8001

則會在8001端口打開http服務(wù)。

使用Web服務(wù)

可以使用http://0.0.0.0:8000/查看www目錄下的網(wǎng)頁文件,若無index.html則會顯示目錄下的文件。

也可以使用ifconfig命令查看本機IP并使用。

補充:python創(chuàng)建http服務(wù)

背景

用java調(diào)用dll的時候經(jīng)常出現(xiàn) invalid memory access,改用java-Python-dll,

Python通過http服務(wù)給java提供功能。

環(huán)境

Python3.7

通過 http.server.BaseHTTPRequestHandler 來處理請求,并返回response

打印日志

filename為輸入日志名稱,默認是同目錄下,沒有該文件會新創(chuàng)建

filemode a 是追加寫的模式,w是覆蓋寫

?
1
2
3
4
5
6
7
8
import logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    filename="hhh.txt",
    filemode='a'
)
logging.info("xxxx")

調(diào)用dll

pchar - ctypes.c_char_p

integer 用了 bytes(0),byref(ctypes.c_void_p(0)) 都OK,沒有更深入去研究,如有錯誤請指正。

?
1
2
3
4
5
6
7
8
9
10
import ctypes
from ctypes import *
dll = ctypes.windll.LoadLibrary('C:\\xxx\\xxx.dll')
print("dll版本號為 : "+ str(dll.GetVersion()) )
 name = ctypes.c_char_p(b"gc")
            roomno = ctypes.c_char_p(bytes(room.encode("utf-8")))
            begintime = ctypes.c_char_p(bytes(begin.encode("utf-8")))
            endtime = ctypes.c_char_p(bytes(end.encode("utf-8")))
            cardno = ctypes.c_void_p(0)
            dll.invoke...

http方案一

要注意 必須有 response = response_start_line + response_headers + “\r\n” + response_body

拼接應(yīng)答報文后,才能給瀏覽器正確返回

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding:utf-8
import socket
from multiprocessing import Process
def handle_client(client_socket):
    # 獲取客戶端請求數(shù)據(jù)
    request_data = client_socket.recv(1024)
    print("request:", request_data)
    # 構(gòu)造響應(yīng)數(shù)據(jù)
    response_start_line = "HTTP/1.1 200 OK\r\n"
    response_headers = "Server: My server\r\n"
    response_body = "helloWorld!"
    response = response_start_line + response_headers + "\r\n" + response_body
    print("response:", response)
    # 向客戶端返回響應(yīng)數(shù)據(jù)
    client_socket.send(bytes(response, "utf-8"))
    # 關(guān)閉客戶端連接
    client_socket.close()
if __name__ == "__main__":
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("", 8888))
    server_socket.listen(120)
    print("success")
    while True:
        client_socket, client_address = server_socket.accept()
        print("[%s, %s]用戶連接上了" % client_address)
        handle_client_process = Process(target=handle_client, args=(client_socket,))
        handle_client_process.start()
        client_socket.close()

完整代碼

另外一種http方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#-.- coding:utf-8 -.-
from http.server import  HTTPServer
import ctypes
from ctypes import *
# HTTPRequestHandler class
import http.server
import socketserver
import logging
# pyinstaller -F
class testHTTPServer_RequestHandler(http.server.BaseHTTPRequestHandler):
    # GET
  def do_GET(self):
        logging.error('start make ')
        str2 =  str(self.path)
        print("revice: " + str2)
        if "xxx" in str2:
            # todo 你的具體業(yè)務(wù)操作
               
            if "xxx" in str2:
                print("hahaha")
                logging.error('hahaha')
                # response_body = "0"
                self.send_response(200)
                # Send headers
                self.send_header('Content-type','text/html')
                self.end_headers()
                # Send message back to client
                message = "Hello world!"
                # Write content as utf-8 data
                self.wfile.write(bytes(message, "utf8"))
                return
        else:
            print("1else")
            self.send_response(200)
            # Send headers
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            # Send message back to client
            message = "Hello world222333!"
            # Write content as utf-8 data
            self.wfile.write(bytes(message, "utf8"))
            return
            
def run():
  print('starting server...')
  logging.basicConfig(
      level=logging.INFO,
      format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
      filename="http_make_card.txt",
      filemode='a+'
  )
  # Server settings
  server_address = ('127.0.0.1', 8888)
  httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
  print('running server...')
  httpd.serve_forever()
run()

打包exe

?
1
pip install pyinstaller

pyinstaller -F xxx.py 即可,當前目錄下生成

1、No module named ‘http.server'; ‘http' is not a package

當時自己建了一個py叫http,刪掉后正常

2、UnicodeDecodeError: ‘utf-8' codec can't decode byte 0xce in position 130: invalid continuat

另存為utf-8即可

Python3 如何開啟自帶http服務(wù)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/SPACESTUDIO/article/details/86760104

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: heyzo1754北岛玲在线视频 | 二区三区在线观看 | 揉搓喷水h | 美女裆部 | 日本高清免费中文字幕不卡 | 日韩日韩日韩手机看片自拍 | 高h全肉动漫在线观看免费 高h辣h双处全是肉军婚 | 亚洲一区二区三区在线播放 | 国产馆 | 日本免费v片一二三区 | 韩国最新理论片奇忧影院 | 久久一本岛在免费线观看2020 | 无人在线观看免费高清视频播放 | 国产精品男人的天堂 | 国产精品极品美女自在线 | 精品无人乱码一区二区三区 | 美女的让男人桶爽网站 | 成人毛片1024你懂的 | 欧美日韩一区二区三区韩大 | 四虎影院久久久 | 男人吃奶动态图 | 国产国语videosex另类 | 精品精品国产自在久久高清 | 我把寡妇日出水好爽 | 天天爽天天干天天操 | 日本最新免费二区 | x8x8国产在线观看2021 | 欧美视频一区二区三区在线观看 | 日本免费一区二区三区四区五六区 | 国内精品久久久久影院嫩草 | 毛片群 | 啊用力好大粗黑人小说 | 性夜a爽黄爽 | 翁用力的抽插 | 日韩在线 在线播放 | 亚洲精品资源 | 图片专区小说专区卡通动漫 | 久久香蕉电影 | 免费全看男女拍拍拍的视频 | 亚洲天天综合 | 精品在线播放 |