利用python+request通過接口實現人員通行記錄上傳功能
腳本中包含以下幾點常用功能:
(1)實時獲取當前時間(2)while循環提交(3)上傳圖片文件
一、上述功能解釋:(1)實時獲取當前時間,下面展示三種格式化后的日期代碼示例
#!/usr/bin/python# -*- coding: UTF-8 -*- import time # 格式化成2016-03-20 11:45:39形式print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式print time.strftime('%a %b %d %H:%M:%S %Y', time.localtime()) # 將格式字符串轉換為時間戳a = 'Sat Mar 28 22:24:24 2016'print time.mktime(time.strptime(a,'%a %b %d %H:%M:%S %Y'))
以上實例輸出結果:
2016-04-07 10:25:09Thu Apr 07 10:25:09 20161459175064.0
詳情查看菜鳥教程,https://www.runoob.com/python/python-date-time.html(2)while循環提交其基本形式為:
while 判斷條件(condition): 執行語句(statements)……
具體查看菜鳥教程,https://www.runoob.com/python/python-while-loop.html(3)上傳圖片文件
filexxxx ={'filexxxxxxxx':open(’xxx.jpg’,’rb’)#文件內容根據實際路徑修改}
具體查看 https://www.jb51.net/article/198278.htm
二、預期結果示例注意:腳本中含有多余的無關代碼信息,我寫在這里只是自我記錄修改userId,mac即可對應上傳不同人員、不同設備的通行記錄
import randomimport timeimport requestsdef test_zhuce(): i=1 while i<1000: url='http://xx.xx.cn:8888/xxxx/robot/uploadVisitorOutIn' url1 = 'https://xxxx.xxxx/xxx/app/2.1.0/token/signxxx' r1=requests.post(url1) t = r1.json()['token'] b=random.randint(1,100000) date = { # 'name': '接口注冊%d' % b, 'userId': '8d92402b9f859d', 'userType' : 5, 'operateType': 1, # 'msToken': t, #實時獲取時間信息 'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), 'mac': 'qwertyuioo', 'ageType':0, 'emotionType':0, 'genderType':0, 'stranger':'false', 'openWay':0, 'temperature':0.0 } #上傳圖片,open(’1610507254344.png’,’rb’)中的1610507254344.png文件是放在了項目內,如果不在項目內,需要添加對應的文件路徑 files={ 'picFile':open(’1610507254344.png’,’rb’), } r = requests.post(url, data=date,files=files) print(’n’'狀態:',r.text) print(’n’'頭部信息:',r.headers) print(’n’'cookie信息:',r.cookies) print(’n’'token信息:',t) i+=1 # assert r.status_code == 200
到此這篇關于利用python+request通過接口實現人員通行記錄上傳功能的文章就介紹到這了,更多相關python request實現人員通行記錄上傳內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
