zoukankan      html  css  js  c++  java
  • 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     #地置数据(位置名称	经度	纬度)当然也可以做成你自己文本格式,容易解析最好
    61     where = open('coordinate', 'r').readlines()
    62     position = [x.strip().split('	') 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)

    代码很多地方偷了懒,特别是对文件操作,大家别学。

  • 相关阅读:
    (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row
    (二叉树 BFS) leetcode513. Find Bottom Left Tree Value
    (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
    (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
    (BFS) leetcode 690. Employee Importance
    (BFS/DFS) leetcode 200. Number of Islands
    (最长回文子串 线性DP) 51nod 1088 最长回文子串
    (链表 importance) leetcode 2. Add Two Numbers
    (链表 set) leetcode 817. Linked List Components
    (链表 双指针) leetcode 142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/donjor/p/4873794.html
Copyright © 2011-2022 走看看