您好,登錄后才能下訂單哦!
昨天學了一天的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作為優先腳本語言,處理簡單問題很快。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。