Python批量发送post请求的实现代码

yipeiwu_com5年前Python基础

昨天学了一天的Python(我的生产语言是java,也可以写一些shell脚本,算有一点点基础),今天有一个应用场景,就正好练手了。

这个功能之前再java里写过,比较粗糙,原来是在我本机跑的,今天老大要求要随时保持请求,就用Python改写了下,省的又把一个有跟多杂项的jar包传到服务器,省空间又不乱,而且好读。

先附上java代码:

package xxxxxx.base;  
import java.util.Random;  
import org.apache.commons.lang3.StringUtils;  
import haojianxiang.util.HttpRequest;  
public class CreateFeedbackData { 
 
  public static void main(String[] args) { 
    while (true) { 
      try { 
        Random r = new Random(); 
        int sleep = r.nextInt(1200000) + 600000; 
        Thread.sleep(sleep); 
        post(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
  } 
   
  public static void post(){ 
    String url = "http://111.111.111.111:8080/xxxx/post"; 
     
    int[] types = {1, 2, 3, 4}; 
    int index = (int) (Math.random() * types.length); 
    int type = types[index]; 
     
//   String[] contents = {"-中文测试-,","-English Test-,","~!@#$%^&*()_;:'-\"<>?/|\\-,","     "}; 
    String[] contents = {"-中文测试-,","-English Test-,","~!@#$%,","     "}; 
     
    StringBuffer content = new StringBuffer(); 
    content.append("haojianxiang test:"); 
    for (int i = 0; i < 10; i++) { 
      int idx = (int) (Math.random() * contents.length); 
      content.append(contents[idx]); 
    } 
     
    String[] imgs = {"/Upload/appUpload/58c7b315cb39f.jpg", 
        "/Upload/appUploa/58cb467a69873.jpg", 
        "/Upload/appUpload/58afff0e99432.png", 
        "/Upload/appUpload/58b545539eb80.jpg", 
        "/Upload/appUpload/58b55d7c9e281.JPG", 
    }; 
    StringBuffer img = new StringBuffer(); 
     
    for (int i = 0; i < (int) (Math.random() * 4); i++) { 
      int lucky = (int) (Math.random() * 2); 
      if (lucky == 1) { 
        int idx = (int) (Math.random() * imgs.length); 
        img.append(imgs[idx]); 
        img.append(","); 
      } 
    } 
    String imgStr = ""; 
    if (StringUtils.isNotBlank(img)) { 
      imgStr = img.substring(0, img.length()-1); 
    } 
    String param = "{\"req\":{\"userId\":xxxxxx}," + 
        "\"data\":{"\"fbType\":" + type + ",\"fbContent\":\""+content.toString()+ 
        "\",\"fbPic\":\""+imgStr+"\"}}"; 
    String rst = HttpRequest.sendPost(url, param); 
    System.out.println("TIME--"+ System.currentTimeMillis() + " result:" + rst); 
  } 
 
} 

(代码里的参数地址等我已做了隐藏,json格式可能不准确了,无所谓)

接下来上Python代码:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
 
import random 
import urllib 
import urllib.request 
import time 
 
def postFeedBack(): 
  url = "http://111.111.111.111:8080/xxxx/post" 
 
  type = int(random.uniform(1,5)) 
  contents = ["-中文测试-,","-English Test-,","~!@#$%,","   "] 
  content = "haojianxiang test:" 
  for i in range(0,10): 
    content += random.choice(contents) 
 
  img = "" 
  imgs = ["/Upload/58c7b315cb39f.jpg", 
      "/Upload/58cb467a69873.jpg", 
      "/Upload/58afff0e99432.png", 
      "/Upload/58b545539eb80.jpg", 
      "/Upload/58b55d7c9e281.JPG"] 
  for i in range(0,3): 
    lucky = int(random.uniform(0,2)) 
    if lucky == 1: 
      img += random.choice(imgs) 
      img += "," 
  img = img[:-1] 
  data = "{\"req\":{\"userId\": xxx}," 
  data += "\"data\":{ 
  data += "\"fbType\":" 
  data += str(type) 
  data += ",\"fbContent\":\"" 
  data += content 
  data += "\",\"fbPic\":\"" 
  data += img 
  data += "\"}}" 
 
  pdata = bytes(data,encoding="utf-8") 
  f = urllib.request.urlopen(url,pdata) 
 
  result = f.read() 
  result = result.decode('UTF-8') 
  print(result) 
 
if __name__ == "__main__": 
  while True: 
    st = int(random.uniform(600,1800)) 
    print("sleep:",st) 
    time.sleep(st) 
    postFeedBack() 

Python的写法确实很简洁高效(java代码里post的逻辑,我还是调用了自己的一个工具类,实际代码要更多),今后打算把Python作为优先脚本语言,处理简单问题很快。

相关文章

django框架使用方法详解

django框架使用方法详解

我的文章的意义 服务端开发,python,django这些内容上面的链接中有详细的阐述. 我写的内容肯定没有上面的完备,准确. 我的文章的价值在于从一个iOS程序员的角度来理解服务端开...

Python中defaultdict与lambda表达式用法实例小结

本文实例讲述了Python中defaultdict与lambda表达式用法。分享给大家供大家参考,具体如下: 从教程中看到defaultdict是一个类,在一台装有Python2.7.6...

Python查找第n个子串的技巧分享

Problem Python中str类自带的find、index方法可以返回第一个匹配的子串的位置,但是如果实际使用中需要查找第2个甚至第n个子串的位置该怎么办呢。也许有的码友可能会用到...

详谈Python2.6和Python3.0中对除法操作的异同

Python中除法有两种运算符:'/'和'//';有三种类型的除法:传统除法、Floor除法和真除法。 X / Y类型: 在Python2.6或者之前,这个操作对于整数运算会省去小数部分...

python实现简单的计时器功能函数

本文实例讲述了python实现简单的计时器功能函数。分享给大家供大家参考。具体如下: 此函数通过python实现了一个简单的计时器动能: ''' Simple Timing Func...