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

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。