Python optparse


Python optparse


optparse的作用

optparse模块主要用来为脚本传递命令参数,采用预先定义好的选项来解析命令行参数

如何使用

只需引入optparse模块即可,执行初始化,实例化一个OptionParser对象,在为命令添加选项

使用举例

from optparse import OptionParser
usage="show something usefull
-- for example: how to use this program"
parser = OptionParser(usage) #带参的话会把参数变量的内容作为帮助信息输出

Read more

CTAGS 支持一门语法


CTAGS 支持一门语法


再~/.ctags 文件中加入以下

Rust

--langdef=Rust
--langmap=Rust:.rs
--regex-Rust=/^[ \t]*(#\[[^\]]\][ \t]*)*(pub[ \t]+)?(extern[ \t]+)?("[^"]+"[ \t]+)?(unsafe[ \t]+)?fn[ \t]+([a-zA-Z0-9_]+)/\6/f,functions,function definitions/
--regex-Rust=/^[ \t]*(pub[ \t]+)?type[ \t]+([a-zA-Z0-9_]+)/\2/T,types,

Read more

hbase Balancer


hbase Balancer


banance 主要有四个命令

banance_switch balancer balancer_enabled balance_rsgroup

banance_switch

功能: 开启或关闭自动banance,返回以前的状态
使用:
balance_switch [true|false]

balancer

功能:手动执行一次整个集群的balance,成功返回true,否则返回false

balancer_enabled

功能:查看当前balancer的状态

balance_rsgroup

功能:只有在开启rsgroup时才会有,手动balance

Read more

variable-precision SWAR algorithm


variable-precision SWAR algorithm

简介:这是一个计算一个数中有多少个二进制1的方法

int swar(uint32_t i)
{
    // (A)
    i = ( i & 0x55555555) + ((i >> 1) & 0x55555555);

    // (B)
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);

    // (C)
    i = (i & 0x0F0F0F0F) + ((i >> 4) &am

Read more

SUID、SGID 和SBIT


SUID SGID

针对二进制文件 setuid(set uid ID upon execution) 让执行该二进制文件的用户有持有该文件的用户的权限

ll /bin/passwd 
-rwsr-xr-x 1 root root 27856 Apr  1 11:57 /bin/passwd

SUID -> 4
SGID -> 2
SBIT -> 1
chmod 4755 filename
chmod u+s testfile
chmod g+s testdir
chmod o+t testdir

setgid(set group ID upon execution)

Read more

Shell 基本运算符


Shell 基本运算符


算数运算符

运算符 说明 举例
+ 加法 `expr $a + $b`
- 减法 `expr $a - $b`
* 乘法 `expr $a * $b`
/ 除法 `expr $a / $b`
% 取余 `expr $a % $b`
= 赋值 $a = $b
== 相等 [ expr $a == $b ]
!= 不相等 [ expr $a != $b ]

关系运算符

运算符 说明 举例
-eq 两个数是否相等,相等返回true [ $a -eq $b ]
-ne 两个数是否不等,不等返回true [ $a -

Read more

IPC共享内存


IPC共享内存涉及到的函数


int shmget(key_t key, size_t size, int shmflg);

该函数用来创建共享内存
第一个参数,与信号量的semget函数一样,程序需要提供一个参数key(非0整数),它有效地为共享内存段命名,
shmget函数成功时返回一个与key相关的共享内存标识符(非负整数),用于后续的共享内存函数。调用失败返回-1.
第二个参数,size以字节为单位指定需要共享的内存容量
第三个参数,shmflg是权限标志,它的作用与open函数的mode参数一样,如果要想在key标识的共享内存不存在时,
创建它的话,可以与IPC_CREAT做

Read more

dup、 dup2 和dup3


简介


#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
int dup3(int oldfd, int newfd, int flags); // 是GNU的扩展,不一定所有系统都支持

// 这些系统调用都返回oldfd的文件描述符的副本
These system calls create a copy of the file descriptor oldfd.

// 新的文件描述符使用未用的最小的文件描述符
dup() uses the lowest-numbered unu

Read more

代码评审


代码评审

代码行

  • 用到变量是否可能为null
  • 是否会有越界访问, 如访问不存在的索引的数组或不存在key的字典
  • 调用函数是否可能抛出异常,或返回错误

函数

  • 每个输入参数是否可空,类型是否正确,范围是否正常
  • 代码行数是否过多

  • 职责是否单一
  • 是否依赖了过多的类
  • 是否有不属于该类抽象层次的函数

安全

代码安全

  • JSONP函数名要编码
  • echo的地方要XSS编码
  • SQL执行要参数化,不能拼
  • redirect的目标地址要限制domain

API安全

  • 写操作要防止CSRF
  • 私有数据访问要鉴权
  • 要涉及越权访问返回码,防止越权访问
  • 资源消耗类API要增加频率限制, 如注册帐号,短信,邮件

Read more