圆月山庄资源网 Design By www.vgjia.com

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

标签:
Python,批量post请求

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。