0%

抓取网页

1
curl https://www.baidu.com

如果发现结果中出现乱码,可以使用iconv转码

1
curl http://iframe.ip138.com/ic.asp|iconv -fgb2312

iconv的用法此处就不详细说明

使用代理

curl使用http代理抓取页面

1
2
curl -x 111.95.243.36:80 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl -x 111.95.243.36:80 -U username:password http://www.baidu.com

使用socks代理抓取页面

1
2
curl --socks4 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl --socks5 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312

代理服务器地址可以从爬虫代理上获取

对Cookie的处理

接受Cookie

1
curl -c /tmp/cookies http://www.baidu.com           #cookies保存到/tmp/cookies文件

发送Cookie

1
2
curl -b "key1=val1;key2=val2;" http://www.baidu.com #发送cookies文本
curl -b /tmp/cookies http://www.baidu.com #从文件中读取cookies

发送数据

Get方法提交数据

1
curl -G -d "name=value&name2=value2" http://www.baidu.com

Post方法提交数据

1
2
curl -d "name=value&name2=value2" http://www.baidu.com  #post数据
curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com #post文件

发送上传文件

1
curl -F file=@/tmp/me.txt http://www.baidu.com

HttpHeader的处理

设置HTTP请求头信息

1
2
3
curl -A "Mozilla/5.0 Firefox/21.0" http://www.baidu.com     #设置http请求头User-Agent
curl -e "http://pachong.org" http://www.baidu.com #设置http请求头Referer
curl -H "Connection:keep-alive \n User-Agent: Mozilla/5.0" http://www.baidu.com

设置HTTP响应头信息

1
2
curl -I http://www.baidu.com #仅仅返回header
curl -D /tmp/header http://www.baidu.com #将http header保存到/tmp/header文件

认证

1
2
curl -u username:password http://www.baidu.com  #用户名密码认证
curl -E mycert.pem https://www.baidu.com #采用证书认证

其他

1
2
curl -# http://www.baidu.com                    #以"#"号输出进度条
curl -o /tmp/baidu http://www.baidu.com #保存http响应到/tmp/baidu

注意事项

HTTP请求地址的URL最好使用双引号 " 括起来,当有多个参数使用 & 连接时可能会出错

Docker在Debian系统中的安装方式

安装前提

  • 删除旧版本
1
apt-get remove docker docker-engine docker.io containerd runc
  • 安装必要软件
1
apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

准备工作

  • 添加 linux/debian/gpg
1
curl -fsSL https://download.docker.com/linux/debian/gpg |  gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • 添加 Debian 软件源
1
2
echo   "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

开始安装

1
apt-get update && apt-get install docker-ce docker-ce-cli containerd.io

安装 Docker-compose

1
2
3
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

Drone 原生就支持docker的CI,所有编译、测试的流程都在 Docker 容器中进行。

环境

  • Debian + Docker

镜像

容器

Step 1,准备

  • Gitea中创建一个OAuth应用
  • 创建一个共享密钥
1
openssl rand -hex 16

Step 2,获取镜像

1
docker pull drone/drone:latest

Step 3,配置

  • DRONE_GITEA_SERVER , Gitea的主机地址(域名)
  • DRONE_GITEA_CLIENT_ID , Gitea生成OAuth的Client ID
  • DRONE_GITEA_CLIENT_SECRET , Gitea生成OAuth的Client Secret
  • DRONE_RPC_SECRET , 生成的共享密钥
  • DRONE_SERVER_HOST , Drone的主机地址(域名,含端口部分)
  • DRONE_SERVER_PROTO , Drone的服务器协议(HTTP/HTTPs)

Step 4,安装Runner

另外详解

Step 5,启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
docker run \
--volume=/var/lib/drone:/data \
--env=DRONE_GITEA_SERVER={{DRONE_GITEA_SERVER}} \
--env=DRONE_GITEA_CLIENT_ID={{DRONE_GITEA_CLIENT_ID}} \
--env=DRONE_GITEA_CLIENT_SECRET={{DRONE_GITEA_CLIENT_SECRET}} \
--env=DRONE_RPC_SECRET={{DRONE_RPC_SECRET}} \
--env=DRONE_SERVER_HOST={{DRONE_SERVER_HOST}} \
--env=DRONE_SERVER_PROTO={{DRONE_SERVER_PROTO}} \
--publish=80:80 \
--publish=443:443 \
--restart=always \
--detach=true \
--name=drone \
drone/drone:1

Electron简单上手

Electron是Github发布的跨平台桌面应用开发工具,基于Web技术(NodeJS + Chromium)

安装

推荐使用npm安装

1
2
3
4
5
# 在全局环境安装`electron`命令
npm install electron-prebuilt -g

# 以依赖的方式安装
npm install electron-prebuilt --save-dev

创建应用

Electron项目的基本结构如下:

1
2
3
4
your-app/
├- package.json
├- main.js
└- index.html

其中package.json的格式和nodejs的模块中package.json完全相同,由main字段指定应用的起始脚本,用来运行主进程。下面是一个package.json的例子:

1
2
3
4
5
{
"name" : "your-app",
"version" : "0.1.0",
"main" : "main.js"
}

提示:如果main字段不存在,Electron会尝试加载index.js
main.js中应该创建窗体并且处理系统事件,下面是一个经典的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';

const electron = require('electron');
const app = electron.app; // 用来控制应用的模块
const BrowserWindow = electron.BrowserWindow; // 创建浏览器窗口的模块

var mainWindow = null;

// 当所有窗口关闭的时候
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});

// Electron加载完成后,该方法会被调用
// 加载完成后创建一个浏览器窗口
app.on('ready', function() {
// 创建一个浏览器窗口
mainWindow = new BrowserWindow({width: 800, height: 600});

// 加载index.html
mainWindow.loadURL('file://' + __dirname + '/index.html');

// 打开开发者工具窗口
mainWindow.webContents.openDevTools();

// 当窗口关闭时,向上冒泡(Closed)事件
mainWindow.on('closed', function() {
mainWindow = null;
});
});

最后index.html是你将看到的web页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>

运行

在应用所在的目录,即package.json所在的目录,运行一下命令:

1
electron .

从远程主机克隆一个版本库到本地

1
git clone -o <远程主机名> <远程版本库网址> <本地目录>

查看远程主机

1
git remote -v

修改远程主机名

1
git remote rename <原主机名> <新主机名>

从远程主机取回更新

1
git fetch <远程主机名> <分支名>

从远程主机的更新却会到新的分支

1
git checkout -b <分支名> <远程主机名>/<分支名>

把远程主机的更新合并到本地分支

1
2
3
git merge <远程主机名>/<分支名>
# 或者
git rebase <远程主机名>/</分支名>

从远程分支合并到本地指定分支

1
git pull <远程主机名> <远程分支名>:<本地分支名>

从推送某个分支到远程主机的指定分支

1
git push <远程主机名> <本地分支名>:<远程分支名>

客户端配置

在客户端生成公钥

1
ssh-keygen -t rsa

服务端配置

公钥生成后会在[HOME_PATH]/.ssh/目录下生成id_rsa.pub文件,复制到服务端上

1
scp ~/.ssh/id_rsa.pub [USER]@[SERVER_IP]:/root/.ssh/[CLIENT_IP]_id_rsa.pub

服务端[HOME_PATH]/.ssh/目录下生成授权列表文件authorized_keys

1
2
3
cd ~/.ssh
touch authorized_keys
chmod 644 authorized_keys

将公钥id_rsa.pub加入到授权文件中

1
cat [CLIENT_IP]_id_rsa.pub >> authorized_keys

注意事项

  • 用scp复制公钥的时候要修改目标用户目录的权限为777,否则权限不够无法复制
  • 服务端的.ssh目录的权限一定是700
  • authorized_keys的权限要是600以上
  • 服务端的用户目录的权限要改回755,否则无法实现免密码登陆

Xhprof是一个分层PHP性能分析工具。

环境

  • Debian + Nginx + PHP-FPM

依赖

  • phpize
  • 安装方法: apt-get install phpize

下载和安装

1
2
3
4
5
6
7
8
wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
sudo phpize
./configure
sudo make
sudo make install
cp xhprof.so [path_to_php_extension]/xhprof.so

配置php.ini

1
2
3
[xhprof]
extension=xhprof.so
xhprof.output_dir=/var/www/xhprof/xhprof_runs/

搭建Xhprof运行环境

1
2
cp -a [path_to_xhprof]/xhprof_html /var/www/xhprof/
cp -a [path_to_xhprof]/xhprof_lib /var/www/xhprof/

注入PHP代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
xhprof_enable();
/**
...
要检查的php代码
...
**/
$xhprof_data = xhprof_disable();

// 引入xhprof_lib
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php";

$instance = new XHProfRuns_Default();
$run_id = $instance->save_run($xhprof_data, "xhprof");
var_dump($run_id);

查看检查结果

  • 配置Nginx
1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name xhprof.test.com;
root /var/www/xhprof/xhprof_html;
index index.html index.php;
location ~ \.php$ {
fastcgi_pass php_pool;
fastcgi_index index.php;
include fastcgi.conf;
}
}

更优雅的方式

  • 新建一个额外的文件 xhprof.inc.php,保存在/var/www/xhprof/目录下
1
2
3
4
5
6
7
8
9
10
11
12
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
register_shutdown_function(function() {
$xhprof_data = xhprof_disable();
if (function_exists('fastcgi_finish_request')){
fastcgi_finish_request();
}
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');
echo 'save xhprof data';
});
  • 利用PHP的自动加载功能,在执行代码前注入此文件,编辑php.ini
1
auto_prepend_file = /var/www/xhprof/xhprof.inc.php
  • 重启PHP服务

个人介绍