亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python批量發送post請求的實現代碼

發布時間:2020-09-01 19:44:16 來源:腳本之家 閱讀:131 作者:mdxy-dxy 欄目:開發技術

昨天學了一天的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作為優先腳本語言,處理簡單問題很快。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

尼勒克县| 尉犁县| 翁牛特旗| 桑植县| 京山县| SHOW| 海南省| 北宁市| 梅州市| 公主岭市| 托克逊县| 义马市| 宜兰市| 丰城市| 灵台县| 榆社县| 萍乡市| 潢川县| 镇雄县| 临夏县| 临西县| 柳林县| 家居| 哈密市| 大荔县| 仁寿县| 蒲城县| 通江县| 苍梧县| 青阳县| 吴桥县| 泽库县| 舒城县| 龙胜| 渝中区| 榕江县| 冷水江市| 金门县| 甘谷县| 昆山市| 林芝县|