亲宝软件园·资讯

展开

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

人气:0
想了解Python批量发送post请求的实现代码的相关内容吗,在本文为您仔细讲解Python批量post请求的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python,批量post请求,下面大家一起来学习吧。

昨天学了一天的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作为优先脚本语言,处理简单问题很快。

加载全部内容

相关教程
猜你喜欢
用户评论