from gevent import monkey; monkey.patch_all()
import bottle
import gevent
def worker():
print ('worker called')
gevent.sleep(10)
print ('worker finished after 10 secondss')
@bottle.route('/')
def index():
gevent.spawn(worker)
return "ok"
def main():finish
bottle.run(host='0.0.0.0',port=8080, server="gevent")
if __name__ == '__main__':
main()
Computer has revolutionized the world and programming is the most important tool to make our dream come true;
Showing posts with label 后台,ubuntu. Show all posts
Showing posts with label 后台,ubuntu. Show all posts
Sunday, July 3, 2016
Friday, July 1, 2016
curl 测速
curl -o /dev/null -s -w 'DNS:%{time_namelookup}\nConnect:%{time_connect}\nStart:%{time_starttransfer}\nTotal:%{time_total}\nSpeed:%{speed_download} byte/s' https://xxx.com/
返回结果:
DNS:0.030
Connect:0.078
Start:0.503
Total:0.515
Speed:60306.000 byte/s
DNS:0.030
Connect:0.078
Start:0.503
Total:0.515
Speed:60306.000 byte/s
SSL延迟有多大?
据说,Netscape公司当年设计SSL协议的时候,有人提过,将互联网所有链接都变成HTTPs开头的加密链接。
这个建议没有得到采纳,原因之一是HTTPs链接比不加密的HTTP链接慢很多。(另一个原因好像是,HTTPs链接默认不能缓存。)
自从我知道这个掌故以后,脑袋中就有一个观念:HTTPs链接很慢。但是,它到底有多慢,我并没有一个精确的概念。直到今天我从一篇文章中,学到了测量HTTPs链接耗时的方法。
首先我解释一下,为什么HTTPs链接比较慢。
HTTPs链接和HTTP链接都建立在TCP协议之上。HTTP链接比较单纯,使用三个握手数据包建立连接之后,就可以发送内容数据了。

上图中,客户端首先发送SYN数据包,然后服务器发送SYN+ACK数据包,最后客户端发送ACK数据包,接下来就可以发送内容了。这三个数据包的发送过程,叫做TCP握手。
再来看HTTPs链接,它也采用TCP协议发送数据,所以它也需要上面的这三步握手过程。而且,在这三步结束以后,它还有一个SSL握手。
总结一下,就是下面这两个式子。
HTTP耗时 = TCP握手HTTPs耗时 = TCP握手 + SSL握手
所以,HTTPs肯定比HTTP耗时,这就叫SSL延迟。
命令行工具curl有一个w参数,可以用来测量TCP握手和SSL握手的具体耗时,以访问支付宝为例。
$ curl -w "TCP handshake: %{time_connect}, SSL handshake: %{time_appconnect}\n" -so /dev/null https://www.alipay.com TCP handshake: 0.022, SSL handshake: 0.064
上面命令中的w参数表示指定输出格式,time_connect变量表示TCP握手的耗时,time_appconnect变量表示SSL握手的耗时,s参数和o参数用来关闭标准输出。
从运行结果可以看到,SSL握手的耗时(64毫秒)大概是TCP握手(22毫秒)的三倍。也就是说,在建立连接的阶段,HTTPs链接比HTTP链接要长3倍的时间,具体数字取决于CPU的快慢和网络状况。
所以,如果是对安全性要求不高的场合,为了提高网页性能,建议不要采用保密强度很高的数字证书。一般场合下,1024位的证书已经足够了,2048位和4096位的证书将进一步延长SSL握手的耗时。
Sunday, June 5, 2016
ubuntu 配置 mysql
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION
sudo ufw allow 3306/tcp
sudo service ufw restart
bind-address 127.0.0.1
sudo /etc/init.d/mysql restart
Thursday, May 12, 2016
redis的使用
前言
Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速。用Redis可以很轻松解决高并发的数据访问问题;做为时时监控信号处理也非常不错。
目录
- Redis在Windows中安装
- Redis在Linux Ubuntu中安装
- 通过命令行客户端访问Redis
- 修改Redis的配置
1. Redis在Windows中安装
在Windows系统上安装Redis数据库是件非常简单的事情,下载可执行安装文件(exe),双击安装即可。下载地址:https://github.com/rgl/redis/downloads
- Redis服务器运行命令:Redis安装目录/redis-server.exe
- Redis客户端运行命令:Redis安装目录/redis-cli.exe
2. Redis在Linux Ubuntu中安装
本文使用的Linux是Ubuntu 12.04.2 LTS 64bit的系统,安装Redis数据库软件包可以通过apt-get实现。
在Linux Ubuntu中安装Redis数据库
#安装Redis服务器端
~ sudo apt-get install redis-server
安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序
# 检查Redis服务器系统进程
~ ps -aux|grep redis
redis 4162 0.1 0.0 10676 1420 ? Ss 23:24 0:00 /usr/bin/redis-server /etc/redis/redis.conf
conan 4172 0.0 0.0 11064 924 pts/0 S+ 23:26 0:00 grep --color=auto redis
# 通过启动命令检查Redis服务器状态
~ netstat -nlt|grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
# 通过启动命令检查Redis服务器状态
~ sudo /etc/init.d/redis-server status
redis-server is running
3. 通过命令行客户端访问Redis
安装Redis服务器,会自动地一起安装Redis命令行客户端程序。
在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。
~ redis-cli
redis 127.0.0.1:6379>
# 命令行的帮助
redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in
"help " for help on
"help " to get a list of possible help topics
"quit" to exit
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
(empty list or set)
基本的Redis客户端命令操作
增加一条字符串记录key1
# 增加一条记录key1
redis 127.0.0.1:6379> set key1 "hello"
OK
# 打印记录
redis 127.0.0.1:6379> get key1
"hello"
增加一条数字记录key2
# 增加一条数字记录key2
set key2 1
OK
# 让数字自增
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3
# 打印记录
redis 127.0.0.1:6379> get key2
"3"
增加一条列表记录key3
# 增加一个列表记录key3
redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1
# 从左边插入列表
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2
# 从右边插入列表
redis 127.0.0.1:6379> RPUSH key3 c
(integer) 3
# 打印列表记录,按从左到右的顺序
redis 127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"
增加一条哈希表记录key4
# 增加一个哈希记表录key4
redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1
# 在哈希表中插入,email的Key和Value的值
redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1
# 打印哈希表中,name为key的值
redis 127.0.0.1:6379> HGET key4 name
"John Smith"
# 打印整个哈希表
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"
增加一条哈希表记录key5
# 增加一条哈希表记录key5,一次插入多个Key和value的值
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK
# 打印哈希表中,username和age为key的值
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"
# 打印完整的哈希表记录
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"
删除记录
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4) "key5"
5) "key1"
# 删除key1,key5
redis 127.0.0.1:6379> del key1
(integer) 1
redis 127.0.0.1:6379> del key5
(integer) 1
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4. 修改Redis的配置
4.1 使用Redis的访问账号
默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redisredis。
用vi打开Redis服务器的配置文件redis.conf
~ sudo vi /etc/redis/redis.conf
#取消注释requirepass
requirepass redisredis
4.2 让Redis服务器被远程访问
默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。
用vi打开Redis服务器的配置文件redis.conf
~ sudo vi /etc/redis/redis.conf
#注释bind
#bind 127.0.0.1
修改后,重启Redis服务器。
~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.
未使用密码登陆Redis服务器
~ redis-cli
redis 127.0.0.1:6379> keys *
(error) ERR operation not permitted
发现可以登陆,但无法执行命令了。
登陆Redis服务器,输入密码
~ redis-cli -a redisredis
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
登陆后,一切正常。
我们检查Redis的网络监听端口
检查Redis服务器占用端口
~ netstat -nlt|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
我们看到从之间的网络监听从 127.0.0.1:3306 变成 0 0.0.0.0:3306,表示Redis已经允许远程登陆访问。
我们在远程的另一台Linux访问Redis服务器
~ redis-cli -a redisredis -h 192.168.1.199
redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"
远程访问正常。通过上面的操作,我们就把Redis数据库服务器,在Linux Ubuntu中的系统安装完成
Subscribe to:
Posts (Atom)
