Python編程獲取終端命令行參數(shù)示例
實現(xiàn)步驟
獲取終端命令行參數(shù),通過使用 sys.argv實現(xiàn)
1.導(dǎo)入sys模塊
import sys
2.獲取命令行參數(shù)
params = sys.argv print(params) print(params[1])
獲取到的為字符串類型,可能需要轉(zhuǎn)換類型再使用
命令行啟動動態(tài)綁定端口號
1.獲取執(zhí)行python程序的終端命令行參數(shù)
sys.argv
2.判斷參數(shù)的類型,設(shè)置端口號必須是整型
if not sys.argv[1].isdigit(): print("啟動命令如下: python3 xxx.py 9090") return port = int(sys.argv[1])
3.給Web服務(wù)器類的初始化方法添加一個端口號參數(shù),用于綁定端口號
def __init__(self, port): self.tcp_server_socket.bind((“”, port))
代碼實現(xiàn)
import socket import threading import sys # 定義web服務(wù)器類 class HttpWebServer(object): def __init__(self, port): # 創(chuàng)建tcp服務(wù)端套接字 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 設(shè)置端口號復(fù)用, 程序退出端口立即釋放 tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 綁定端口號 tcp_server_socket.bind(("", port)) # 設(shè)置監(jiān)聽 tcp_server_socket.listen(128) # 保存創(chuàng)建成功的服務(wù)器套接字 self.tcp_server_socket = tcp_server_socket # 處理客戶端的請求 @staticmethod def handle_client_request(new_socket): # 代碼執(zhí)行到此,說明連接建立成功 recv_client_data = new_socket.recv(4096) if len(recv_client_data) == 0: print("關(guān)閉瀏覽器了") new_socket.close() return # 對二進制數(shù)據(jù)進行解碼 recv_client_content = recv_client_data.decode("utf-8") print(recv_client_content) # 根據(jù)指定字符串進行分割, 最大分割次數(shù)指定2 request_list = recv_client_content.split(" ", maxsplit=2) # 獲取請求資源路徑 request_path = request_list[1] print(request_path) # 判斷請求的是否是根目錄,如果條件成立,指定首頁數(shù)據(jù)返回 if request_path == "/": request_path = "/index.html" try: # 動態(tài)打開指定文件 with open("static" + request_path, "rb") as file: # 讀取文件數(shù)據(jù) file_data = file.read() except Exception as e: # 請求資源不存在,返回404數(shù)據(jù) # 響應(yīng)行 response_line = "HTTP/1.1 404 Not Found\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" with open("static/error.html", "rb") as file: file_data = file.read() # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) else: # 響應(yīng)行 response_line = "HTTP/1.1 200 OK\r\n" # 響應(yīng)頭 response_header = "Server: PWS1.0\r\n" # 響應(yīng)體 response_body = file_data # 拼接響應(yīng)報文 response_data = (response_line + response_header + "\r\n").encode("utf-8") + response_body # 發(fā)送數(shù)據(jù) new_socket.send(response_data) finally: # 關(guān)閉服務(wù)與客戶端的套接字 new_socket.close() # 啟動web服務(wù)器進行工作 def start(self): while True: # 等待接受客戶端的連接請求 new_socket, ip_port = self.tcp_server_socket.accept() # 當(dāng)客戶端和服務(wù)器建立連接程,創(chuàng)建子線程 sub_thread = threading.Thread(target=self.handle_client_request, args=(new_socket,)) # 設(shè)置守護主線程 sub_thread.setDaemon(True) # 啟動子線程執(zhí)行對應(yīng)的任務(wù) sub_thread.start() # 程序入口函數(shù) def main(): print(sys.argv) # 判斷命令行參數(shù)是否等于2, if len(sys.argv) != 2: print("執(zhí)行命令如下: python3 xxx.py 8000") return # 判斷字符串是否都是數(shù)字組成 if not sys.argv[1].isdigit(): print("執(zhí)行命令如下: python3 xxx.py 8000") return # 獲取終端命令行參數(shù) port = int(sys.argv[1]) # 創(chuàng)建web服務(wù)器對象 web_server = HttpWebServer(port) # 啟動web服務(wù)器進行工作 web_server.start() if __name__ == '__main__': main()
以上就是Python編程獲取終端命令行參數(shù)示例的詳細(xì)內(nèi)容,更多關(guān)于Python獲取終端命令行參數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python+matplotlib繪制簡單的海豚(頂點和節(jié)點的操作)
這篇文章主要介紹了python+matplotlib繪制簡單的海豚(頂點和節(jié)點的操作),具有一定借鑒價值,需要的朋友可以參考下2018-01-01Python使用pydub模塊轉(zhuǎn)換音頻格式以及對音頻進行剪輯
這篇文章主要給大家介紹了關(guān)于Python使用pydub模塊轉(zhuǎn)換音頻格式以及對音頻進行剪輯的相關(guān)資料pydub是python的高級一個音頻處理庫,可以讓你以一種不那么蠢的方法處理音頻。需要的朋友可以參考下2021-06-06Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
這篇文章主要介紹了Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Python趣味挑戰(zhàn)之教你用pygame畫進度條
pygame四種方法教會你畫進度條,其實也不難,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全
柱狀圖(Bar Plot)是一種常用的數(shù)據(jù)可視化方式,用于顯示各個類別之間的比較,下面這篇文章主要給大家介紹了關(guān)于python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全,需要的朋友可以參考下2024-03-03