利用python+request通過接口實(shí)現(xiàn)人員通行記錄上傳功能
腳本中包含以下幾點(diǎn)常用功能:
(1)實(shí)時(shí)獲取當(dāng)前時(shí)間(2)while循環(huán)提交(3)上傳圖片文件
一、上述功能解釋:(1)實(shí)時(shí)獲取當(dāng)前時(shí)間,下面展示三種格式化后的日期代碼示例
#!/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()) # 將格式字符串轉(zhuǎn)換為時(shí)間戳a = 'Sat Mar 28 22:24:24 2016'print time.mktime(time.strptime(a,'%a %b %d %H:%M:%S %Y'))
以上實(shí)例輸出結(jié)果:
2016-04-07 10:25:09Thu Apr 07 10:25:09 20161459175064.0
詳情查看菜鳥教程,https://www.runoob.com/python/python-date-time.html(2)while循環(huán)提交其基本形式為:
while 判斷條件(condition): 執(zhí)行語句(statements)……
具體查看菜鳥教程,https://www.runoob.com/python/python-while-loop.html(3)上傳圖片文件
filexxxx ={'filexxxxxxxx':open(’xxx.jpg’,’rb’)#文件內(nèi)容根據(jù)實(shí)際路徑修改}
具體查看 https://www.jb51.net/article/198278.htm
二、預(yù)期結(jié)果示例注意:腳本中含有多余的無關(guān)代碼信息,我寫在這里只是自我記錄修改userId,mac即可對應(yīng)上傳不同人員、不同設(shè)備的通行記錄
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, #實(shí)時(shí)獲取時(shí)間信息 '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文件是放在了項(xiàng)目內(nèi),如果不在項(xiàng)目內(nèi),需要添加對應(yīng)的文件路徑 files={ 'picFile':open(’1610507254344.png’,’rb’), } r = requests.post(url, data=date,files=files) print(’n’'狀態(tài):',r.text) print(’n’'頭部信息:',r.headers) print(’n’'cookie信息:',r.cookies) print(’n’'token信息:',t) i+=1 # assert r.status_code == 200
到此這篇關(guān)于利用python+request通過接口實(shí)現(xiàn)人員通行記錄上傳功能的文章就介紹到這了,更多相關(guān)python request實(shí)現(xiàn)人員通行記錄上傳內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. js實(shí)現(xiàn)跳一跳小游戲2. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站3. SpringBoot 開發(fā)提速神器 Lombok+MybatisPlus+SwaggerUI4. php5.6不能擴(kuò)展redis.so的解決方法5. js實(shí)現(xiàn)貪吃蛇小游戲(加墻)6. 使用idea 去除 html 代碼前的行號(hào)和空行的方法詳解7. JVM之class文件結(jié)構(gòu)8. Python編寫nmap掃描工具9. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】10. python 爬取嗶哩嗶哩up主信息和投稿視頻
