python通過接口上傳圖片造測試數據


最近客戶端上有個發送圖文的需求,大致是添加圖片,文本數據然后發送。

一般像這樣的情況都是有個單獨上傳圖片的接口,返回上傳圖片的路徑,發送信息的接口接收這個圖片地圖參數,信息存儲的數據庫。

測試中期需要造一些數據來進行測試及觀察整體效果,圖片上傳是個很費事的手機活,那么我又想到了python http神器requests

還是直接上代碼加注釋吧

 1 #-*-coding:utf-8 -*-
 2 __author__ = 'DongJie'
 3 import requests
 4 import glob
 5 import os
 6 import time
 7 import random
 8 import urllib
 9 import sys
10 reload(sys)
11 sys.setdefaultencoding('utf-8')
12 
13 #選擇測試(從測試圖片目錄隨機選擇多張圖片)
14 def chosePic(number):
15     pic_list = glob.glob('E:\\testpic\\*.jpg')
16     up_pic = random.sample(pic_list, number)
17     return up_pic
18 
19 
20 #上傳測試圖片(通過接口將圖片上傳至服務器,得到服務器返回的路徑:http圖片上傳是以二進制附件流上傳到服務器的)
21 def upPic(pic_list):
22     up_url = '127.0.0.1/api/User?sort=tourDating'
23     re = []
24     for pic in pic_list:
25         f = open(u'%s' % pic, 'rb')
26         files = {'file':[os.path.split(pic)[-1], f, 'application/octet-stream']}
27         req = requests.post(up_url, files=files)
28         server_path = req.json()[0]
29         re.append(server_path)
30     path = ','.join(re)
31     return path
32 
33 
34 #這是一個應該用接口
35 def sendYue(account, content, startTime, endTime, lat, lng, address, isNearVisible, picList, contactInformation):
36     add = "http://127.0.0.1/api/TourDating/Publish"
37     value = {'accountId':account,
38              'content':content,
39              'startTime':startTime,
40              'endTime':endTime,
41              'lat':lat,
42              'lng':lng,
43              'address':address,
44              'isNearVisible':isNearVisible,
45              'picList':picList,
46              'contactInformation':contactInformation}
47     #必須用urlencode將參數值編碼
48     args = urllib.urlencode(value)
49     send_url = add + '?' + args
50     try:
51         req = requests.post(send_url)
52         return req.json()
53     except Exception, e:
54         print e
55 
56 
57 if __name__ == "__main__":
58     #從account取已經從數據庫取出來用戶ID數據
59     account_list = open('account', 'r').readlines()
60     #地置數據(位置名稱\t經度\t緯度)當然也可以做成你自己文本格式,容易解析最好
61     where = open('coordinate', 'r').readlines()
62     position = [x.strip().split('\t') for x in where if x!='']
63     content_all = open('content', 'r').read()
64     for x in range(180):
65         account = account_list[x].strip()
66         address = position[x][0]
67         lng = position[x][1]
68         lat = position[x][2]
69         #內容也是從一個文本里面隨機(10-140個字)這個根據自己的需要
70         content = ''.join(random.sample(content_all.decode('utf-8'), random.randint(10, 140)))
71         #下面是我隨機開始時間和結束時間的方法(從一個時間戳段中取一個值,然后往后推隨機天數)
72         t = random.randint(1433541966,1451581261)
73         startTime = time.strftime('%Y-%m-%d', time.localtime(t))
74         endTime = time.strftime('%Y-%m-%d',time.localtime(t+random.randint(1,60)*86400))
75         #這個參數也是一個隨機數了
76         contactInformation = random.randint(111111,19999999999)
77         #0和1隨機取一個
78         isNearVisible = random.randint(0,1)
79         #上傳圖片返回的路徑在這里用到
80         picList = upPic(chosePic(random.randint(1,3)))
81         #發送請求造數據
82         sendYue(account, content, startTime, endTime, lat, lng, address, isNearVisible, picList, contactInformation)

代碼很多地方偷了懶,特別是對文件操作,大家別學。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM