利用python+request通過接口實現(xiàn)人員通行記錄上傳功能
前言:
腳本中包含以下幾點常用功能:
(1)實時獲取當前時間
(2)while循環(huán)提交
(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())
# 將格式字符串轉(zhuǎn)換為時間戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
以上實例輸出結(jié)果:
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.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ù)實際路徑修改
}
具體查看 http://m.fzitv.net/article/198278.htm
二、預期結(jié)果示例


三、完整腳本示例:
注意:腳本中含有多余的無關(guān)代碼信息,我寫在這里只是自我記錄
修改userId,mac即可對應(yīng)上傳不同人員、不同設(shè)備的通行記錄
import random
import time
import requests
def 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文件是放在了項目內(nèi),如果不在項目內(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通過接口實現(xiàn)人員通行記錄上傳功能的文章就介紹到這了,更多相關(guān)python request實現(xiàn)人員通行記錄上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pandas?DataFrame列快速轉(zhuǎn)換為列表(3秒學會!)
這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame列如何快速轉(zhuǎn)換為列表的相關(guān)資料,在Python的pandas庫中可以使用DataFrame的tolist()方法將DataFrame轉(zhuǎn)化為列表,需要的朋友可以參考下2023-10-10
Python+Opencv實現(xiàn)計算閉合區(qū)域面積
這篇文章主要介紹了利用Python?Opencv計算閉合區(qū)域的面積的原理以及實現(xiàn)代碼,文中的講解詳細易懂,感興趣的小伙伴快跟隨小編一起學習一下吧2022-03-03

