1. 程式人生 > 程式設計 >利用python+request通過介面實現人員通行記錄上傳功能

利用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:09
Thu Apr 07 10:25:09 2016
1459175064.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 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檔案是放在了專案內,如果不在專案內,需要新增對應的檔案路徑
 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實現人員通行記錄上傳內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!