Blog

  • python ip地址排序

    方法一:

    ip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']
    ip_list.sort(lambda x,y: cmp(''.join( [ i.rjust(3, '0') for i in x.split('.')] ), ''.join( [ i.rjust(3, '0') for i in y.split('.')] ) ) )
    

    结果

    ['192.168.1.100', '192.168.8.1', '192.168.10.3']

    方法二:

    转换成 int 型,用 int 来比较

    import struct
    import socket
    
    def ip2int(addr):
        return struct.unpack("!I", socket.inet_aton(addr))[0]
    
    def int2ip(addr):
        return socket.inet_ntoa(struct.pack("!I", addr))
    
    ip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']
    # 构建 int_ip:ip 形式的 key:value, 并对 key 排序
    
    ip_unsorted_dict = {}
    for ip in ip_list:
        int_ip = ip2int(ip)
        ip_unsorted_dict[int_ip] = ip
    
    keys = ip_unsorted_dict.keys()
    keys.sort()
    ip_sorted_list=[]
    for key in keys:
        ip_sorted_list.append(ip_unsorted_dict[key])
    
    print ip_sorted_list

    结果:

    ['192.168.1.100', '192.168.8.1', '192.168.10.3']

    参考:
    http://blog.csdn.net/hong201/article/details/3119519

  • nginx代理websocket

    map $http_upgrade $connection_upgrade {
     default upgrade;
     '' close;
    }
    
    upstream proxy_stomp {
     server 192.168.1.1:3000;
    }
    
    server {
     listen 3001;
     server_name proxy.abc.com;
     location / {
     proxy_pass http://proxy_stomp;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     }
    }

    upgrade 意思为升级为其他协议

    http://nginx.org/en/docs/http/websocket.html

  • rabbitmq3.3.1允许guest远程访问

    rabbitmq 默认允许 guest 本地访问
    为了方便需要允许远程访问

    把 /usr/share/doc/rabbitmq-server-3.3.1/rabbitmq.config.example 复制一份到 /etc/rabbitmq/rabbitmq.config,在 rabbit 段中,按如下格式

    [{rabbit, [{loopback_users, []}]}]

    参考:

    https://www.rabbitmq.com/access-control.html

  • 2015读书清单

    目标30本书

    开始时间 技术书 非技术书 教学视频
    2015-01-01 数学之美
    2015-01-19 乱时候,穷时候
    2015-05-17 图解HTTP
    2015-11-4 pro git
  • paste命令

    要把

    a
    b
    c
    d
    e

    变成

    a b c d e

    使用 paste 命令

    paste -s -d' ' filename

    paste 可以将多个文件整合,按列整合

    文件1  file1

    a

    b

    文件2 file2

    1

    2

      ~ paste file1 file2

    a 1

    b 2

      ~ paste -d’ ‘ file1 file2

    a 1

    b 2

    也可以变成

      ~ paste -s file1 file2

    a b

    1 2

    如果想把 奇偶行 拼接成一行,文件为

    1 foo
    2 bar
    3 foo
    4 bar
    5 foo
    6 bar
    7 foo
    8 bar
    

    命令为

    cat filename | paste -d, - -
    1 foo,2 bar
    3 foo,4 bar
    5 foo,6 bar
    7 foo,8 bar

    如果把三行并成一行

    cat filename | paste -d, - - -
  • 配置rsync服务

    要求:配置针对单一ip的免认证 rsync 服务,环境为 centos 6.5,需要关闭 SELINUX

    安装

    yum install -y xinetd rsync

    检查 iptables 需要使 873 端口通行

    配置 xinetd

    vim /etc/xinetd.d/rsync
    # default: off
    # description: The rsync server is a good addition to an ftp server, as it \
    #	allows crc checksumming etc.
    service rsync
    {
    	disable	= no
    	flags		= IPv4
    	socket_type     = stream
    	wait            = no
    	user            = root
    	server          = /usr/bin/rsync
    	server_args     = --daemon
    	log_on_failure  += USERID
    }
    

    配置 rsync 配置文件

    vim /etc/rsyncd.conf
    max connections = 5
    log file = /var/log/rsync.log
    uid = nobody
    gid = nobody
    [web]
    path = /home/ftpuser1
    read only = false
    hosts allow = 192.168.1.1
    

    重启 xinetd

    上面访问的路径就为 rsync -av 192.168.x.x::web .

    参考:

    Setup Rsync in 5 Minutes – Centos 6

    如果不使用 xinetd 来做守护。创建如下目录和文件

    /etc/rsyncd

    ├── rsyncd.conf

    └── rsyncd.secrets

    查看 rsyncd.conf

    pid file = /var/run/rsyncd.pid
    port = 873
    address = 192.168.1.123
    uid = root
    gid = root
    use chroot = yes
    read only = no
    hosts allow=192.168.1.0/255.255.255.0
    hosts deny=*
    max connections = 10
    motd file = /etc/rsyncd/rsyncd.motd
    log format = %t %a %m %f %b
    syslog facility = local3
    timeout = 300
    [bbs]
    path = /data/www/bbs
    list=yes
    ignore errors
    auth users = my_name
    secrets file = /etc/rsyncd/rsyncd.secrets
    comment = blog
    

    查看 rsyncd.secrets

    my_name:mypass

    之后要想传文件到 192.168.1.123 的 /data/www/bbs 下

    客户端创建文件 /etc/sersync/rsync_password
    写入
    mypass
    权限为 0400

    执行

    rsync -a -R --delete ./ --include=bbbb --exclude=* [email protected]::bbs --password-file=/etc/sersync/rsync_password

    关于权限问题而不成功参考:
    http://superuser.com/questions/243656/how-to-configure-and-use-rsyncd

    rsyncd 的 init 文件

    #! /bin/bash
    #
    # chkconfig:   2345 50 50
    # description: The rsync daemon
    #pidfile: /var/run/rsyncd.pid
    
    # source function library
     . /etc/rc.d/init.d/functions
    
    PROG='/usr/bin/rsync'
    BASE=${0##*/}
    
    # The config file must contain following line:
    #  pid file = /var/run/rsync.pid
    OPTIONS="--daemon --config=/etc/rsyncd/rsyncd.conf"
    
    case "$1" in
      start)
        echo -n $"Starting $BASE: "
        daemon $PROG $OPTIONS
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASE
        echo
        ;;
      stop)
        echo -n $"Shutting down $BASE: "
        killproc $PROG
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASE
        echo
        ;;
      restart|force-reload)
        $0 stop
        sleep 1
        $0 start
        ;;
      status)
        status $PROG
        ;;
      *)
        echo "Usage: $0 {start|stop|restart|status|force-reload}" >&2
        exit 1
        ;;
    esac