昨天學了一天的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作為優先腳本語言,處理簡單問題很快。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

