python-生成CURL命令
import json
def getCurlCommand(path, params, method, headers):
if method == 'GET':
getParams = []
for k in params:
getParams.append(k + "=" + params[k])
path += "?" + '&'.join(getParams)
curlCommand = "curl --location --request "+method+" '"+path+"' \\\n"
for k in headers:
curlCommand += "--header '{}: {}' \\\n".format(k, headers[k])
if params:
curlCommand += "--data-raw '"+json.dumps(params)+"'"
print(curlCommand)
headers = {
'Bearer Token': 'XXX',
}
method = 'POST'
path = 'http://baidu.com'
params = {
"staff": '1',
}
getCurlCommand(path, params, method, headers)
python-请求数据生成json和excel文件
import pandas as pd
import json
import requests
url = "https://test1.com"
headers = {
"content-type": "application/json",
}
payload = {"username": "", "password": ""}
getJson = json.dumps(payload).encode("utf-8")
response = requests.post(url=url, headers=headers, data=getJson)
resJson = json.loads(response.text)
url = "https://test2.com"
headers = {
"content-type": "application/json",
"Authorization": "Bearer " + resJson['payload']['token']
}
payload = {
"sql": "select * from table;",
"data_base": "zh"
}
getJson = json.dumps(payload).encode("utf-8")
response = requests.post(url=url, headers=headers, data=getJson)
resJson = json.loads(response.text)
with open("/Users/zh/Downloads/response.json", "w") as f:
json.dump(resJson['payload']['data'], f)
with open('/Users/zh/Downloads/response.json') as data:
df = pd.DataFrame(json.load(data))
df.to_excel('/Users/zh/Downloads/data.xlsx')
python-excel转sql
from itertools import count
import xlrd
def open_excel(file):
try:
data = xlrd.open_workbook(file)
return data
except Exception as e:
print(str(e))
def excel_by_name(file, by_name='Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nRows = table.nrows
tableName = '`zh`.`test`'
arr = table.row_values(0)
del(arr[0])
idIndex = arr.index("id")
del(arr[idIndex])
fields = '`, `'.join(arr)
index = arr.index("deleted_at")
sql = "INSERT INTO " + tableName + " (`"+fields+"`) VALUES "
for i in range(1, nRows):
row_vals = table.row_values(i)
del(row_vals[0])
del(row_vals[idIndex])
if len(row_vals[index]) == 0:
row_vals[index] = 'null'
fields = "', '".join(row_vals)
sql += "('" + fields + "'), "
sql = sql.replace("'null'", "null")
print(sql.rstrip(", ") + ";")
excel_by_name('/Users/zh/Downloads/data.xlsx')
python-读取excel
import xlrd
import datetime
def open_excel(file):
try:
data = xlrd.open_workbook(file)
return data
except Exception as e:
print(str(e))
def excel_by_name(file, by_name='Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nRows = table.nrows
sql = "INSERT INTO `zh`.`test`(`is_delete`, `created_at`, `updated_at`) VALUES "
for i in range(1, nRows):
row_vals = table.row_values(i)
date = xlrd.xldate.xldate_as_datetime(
row_vals[5], 0)
sql += "({}, '{}', '{}'), ".format(int(row_vals[1], date, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(sql.rstrip(", ") + ";")
excel_by_name("/Users/zh/Downloads/aaa.xlsx")
php-kafka
public function consumer(Request $request)
{
$config = new Conf();
$config->set("group.id", "php_consumer");
$config->set('metadata.broker.list', '127.0.0.1:9092,127.0.0.1:9093');
$config->set('enable.auto.commit', 'false');
$config->set('auto.offset.reset', 'earliest');
$consumer = new \RdKafka\KafkaConsumer($config);
$consumer->subscribe(['zh_test']);
while (true) {
$msg = $consumer->consume(120*1000);
if (null === $msg || $msg->err === RD_KAFKA_RESP_ERR__PARTITION_EOF) {
continue;
} elseif ($msg->err) {
Logger::audit("Kafka Consumer错误信息:" . $msg->errstr());
break;
} else {
$value = $msg->payload;
$json = json_decode($value, true);
var_dump($json);
$consumer->commit($msg);
}
}
}
public function producer(Request $request)
{
$config = new \RdKafka\Conf();
$config->set("group.id", "php_producer");
$config->set('metadata.broker.list', '127.0.0.1:9092,127.0.0.1:9093');
$producer = new \RdKafka\Producer($config);
$topic = $producer->newTopic('zh_test');
$topic->produce(-1, 0, '2222');
$result = $producer->flush(10000);
var_dump($result);
}
php-图片打文字水印
function textwater($source, $target, $water_text='test', $water_w=250, $water_h=210, $font_size=14, $angle=-45, $alpha=80, $over_flag=false) {
if (!file_exists($source)) return false;
$info = getimagesize($source);
$type = image_type_to_extension($info[2],false);
$fun = "imagecreatefrom".$type;
$image = $fun($source);
$color = imagecolorallocatealpha($image, 0, 0, 0, $alpha);
$x_length = $info[0];
$y_length = $info[1];
$font = 'statics/fonts/SourceHanSansCN-Normal.ttf';
$water_text = mb_convert_encoding($water_text, "html-entities", "utf-8");
for ($x = 10; $x < $x_length; $x) {
for ($y = 10; $y < $y_length; $y) {
imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $water_text);
$y += $water_h;
}
$x += $water_w;
}
$fun = "image".$type;
$fun($image, $target);
imagedestroy($image);
return true;
}
mysql-全文索引匹配过半不显示-布尔全文搜索模式
WHERE MATCH(title) AGAINST ('mysql' IN BOOLEAN MODE);
mysql-数据导出
mysql -uroot -proot -e "select userid, email from member order by userid" > /home/zh/test.xls