日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 cgi传数据

發布時間:2024/10/12 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 cgi传数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用cgi向文本或者數據庫寫入數據

1.開啟cgi服務

python -m http.server ?--cgi port[端口可選默認為8000]

update.py代碼

#coding:utf8
import cgi
c = cgi.FieldStorage()#獲取表單參數
try:
data1 = c['mem'].value#獲取表單中mem的值
data2 = c['cpu'].value#獲取表單中cpu的值
except KeyError:#如果沒獲取到數據,就把數據設置為空
data1 = ''
data2 = ''
if data1 or data2:#判斷是否獲取到數據
f = open('cgi-bin/1.txt','w',encoding='utf8')#創建文件
f.write(data1 +'%' + '\n')#寫入mem的值
f.write(data2 +'%' + '\n')#寫入cpu的值
f.close()#關閉文件
客戶端代碼: #coding:utf8
import psutil
import urllib.parse
import urllib.request
test_data = {'mem':psutil.virtual_memory().percent,'cpu':psutil.cpu_percent()}
test_data_urlencode = urllib.parse.urlencode(test_data).encode('utf8')#把字典轉為urlencode格式并解碼為字節流
requrl = "http://127.0.0.1:8000/cgi-bin/updata.py"
req = urllib.request.Request(url=requrl,data=test_data_urlencode)#url帶參數去請求服務器
res_data = urllib.request.urlopen(req)#提交請求

客戶端調用cgi的update.py腳本把要傳輸的數據通過固定的格式傳輸到文本文件

轉載于:https://www.cnblogs.com/xianyin/p/6042490.html

總結

以上是生活随笔為你收集整理的python3 cgi传数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。