您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在Python3 中開(kāi)啟自帶的http服務(wù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Python中自帶了簡(jiǎn)單的服務(wù)器程序,能較容易地打開(kāi)服務(wù)。
在python3中將原來(lái)的SimpleHTTPServer命令改為了http.server,使用方法如下:
1. cd www目錄
2. python -m http.server
開(kāi)啟成功,則會(huì)輸出“Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …”,表示在本機(jī)8000端口開(kāi)啟了服務(wù)。
如果需要后臺(tái)運(yùn)行,可在命令后加"&"符號(hào),Ctrl+C不會(huì)關(guān)閉服務(wù),如下:
python -m http.server &
如果要保持服務(wù),則在命令前加nohup以忽略所有掛斷信號(hào),如下:
nohup python -m http.server 8001
如果不使用默認(rèn)端口,可在開(kāi)啟時(shí)附帶端口參數(shù),如:
python -m http.server 8001
則會(huì)在8001端口打開(kāi)http服務(wù)。
可以使用http://0.0.0.0:8000/查看www目錄下的網(wǎng)頁(yè)文件,若無(wú)index.html則會(huì)顯示目錄下的文件。
也可以使用ifconfig命令查看本機(jī)IP并使用。
補(bǔ)充:python創(chuàng)建http服務(wù)
用java調(diào)用dll的時(shí)候經(jīng)常出現(xiàn) invalid memory access,改用java-Python-dll,
Python通過(guò)http服務(wù)給java提供功能。
Python3.7
通過(guò) http.server.BaseHTTPRequestHandler 來(lái)處理請(qǐng)求,并返回response
filename為輸入日志名稱(chēng),默認(rèn)是同目錄下,沒(méi)有該文件會(huì)新創(chuàng)建
filemode a 是追加寫(xiě)的模式,w是覆蓋寫(xiě)
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")
pchar - ctypes.c_char_p
integer 用了 bytes(0),byref(ctypes.c_void_p(0)) 都OK,沒(méi)有更深入去研究,如有錯(cuò)誤請(qǐng)指正。
import ctypes from ctypes import * dll = ctypes.windll.LoadLibrary('C:\\xxx\\xxx.dll') print("dll版本號(hào)為 : "+ 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...
要注意 必須有 response = response_start_line + response_headers + “\r\n” + response_body
拼接應(yīng)答報(bào)文后,才能給瀏覽器正確返回
# coding:utf-8 import socket from multiprocessing import Process def handle_client(client_socket): # 獲取客戶(hù)端請(qǐng)求數(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) # 向客戶(hù)端返回響應(yīng)數(shù)據(jù) client_socket.send(bytes(response, "utf-8")) # 關(guān)閉客戶(hù)端連接 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]用戶(hù)連接上了" % client_address) handle_client_process = Process(target=handle_client, args=(client_socket,)) handle_client_process.start() client_socket.close()
另外一種http方式
#-.- 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()
pip install pyinstaller
pyinstaller -F xxx.py 即可,當(dāng)前目錄下生成
1、No module named ‘http.server'; ‘http' is not a package
當(dāng)時(shí)自己建了一個(gè)py叫http,刪掉后正常
2、UnicodeDecodeError: ‘utf-8' codec can't decode byte 0xce in position 130: invalid continuat
另存為utf-8即可
看完上述內(nèi)容,你們對(duì)怎么在Python3 中開(kāi)啟自帶的http服務(wù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。