Month: 五月 2017

Linux 命令行网络相关命令学习

有待整理。。。

# fuser

查看哪个程序占用了给定端口

“`
fuser XXX/tcp # see which program is using tcp port XXX
fuser XXX/upd # see which program is using udp port XXX
fuser -k XXX/tcp # kill the program, if you have right permissions
“`

# curl

wget 功能很有限,httpie 虽然比较人性化但是有很多 bug,还是 curl 比较好用。

“`
curl -L/–follow http://example.com follow redirect
curl -I/–head http://example.com only headers
curl -o/–output file http://example.com http -d
curl -v/–verbose http://example.com
curl –data “key=value” http://example.com
curl –data-urlencode “key=
curl -X/–request GET/POST http://example.com
curl -H/–header “Accept: utf-8”
curl –referer http://x.com http://example.com
curl –user-agent
“`
curl –cookie

## 使用代理

“`
curl -x ://:@: –proxy-anyauth
“`

## cookies

cookies 功能主要通过两个选项实现 `-b/–cookie` 和 `-c/–cookie-jar`. `–cookie` 用于发送请求时
携带 Cookies, `–cookie-jar` 会把返回的 Cookie 按照 cookie.txt 规范存储到给定的文件中.

“`
curl -v –cookie “USER_TOKEN=Yes” http://127.0.0.1:5000/
curl -v –cookie example.txt http://example.com
“`

需要注意的是, 如果 `–cookie` 的参数中包含了 `=`, 他就会被当做一个键值对来处理, 而且可以
使用多个 Cookie, 用分号分开即可: “key1=val1;key2=val2;…”. 否则的话, 这个参数会被当做文
件名, 会读取对应文件的 Cookie, 这个文件同样要遵守 cookie.txt 的协议.

# 查看网络情况

需要注意的是,在 Linux 上 netstat 已经废弃了,应该使用新的 ss 命令。不过在 macOS 等Unix系统中,还是只有netstat

“`
netstat -a list all ports
netstat -at list all tcp ports
netstat -l all listening ports
“`

netstat -in 查看提供网络接口的信息

netstat -rn 显示路由表

ifconfig eth0 显示接口的状态

ifconfig -a 所有接口的状态

ip addr eth0 全新的命令

Mac

和 Linux 基本相同, 但是缺少 ip 指令, 需要安装 iproute2mac 包

traceroute

nslookup

host

wget –noproxy

mtr

bt download

sudo add-apt-repository ppa:t-tujikawa/ppa
sudo apt-get update
sudo apt-get install aria2

# 使用 rsync 同步

rsync -azP local_dir remote_dir

* `-a` archive,表示归档
* `-n` dry-run,只显示要执行的操作而不具体执行
* `-z` compress,压缩
* `-P` –progress + –partial,显示进度同时断点续传
* `–exclude=

` exclude directory from being rsynced

默认情况下,rsync 使用增量同步,而不会删除文件。使用 `–delete` 删除文件

# 参考资料

1. https://curl.haxx.se/docs/http-cookies.html

redis 实战总结

## redis是做什么的

一个数据结构存储器,数据驻留在内存里,可以在程序的两次之间保存数据

## 一些实现细节和比较好的地方

redis 的 string 是 binary-safe 的,可以存储任意的二进制数据(bytes),甚至可以把图片存储在 redis 中

## 经常用到的场合

1. 用作缓存

1. 最基础的,最经典的应用场合,当查询数据库或者ES等存储代价比较高的时候,直接用查询的语句做 key,查询结果用作缓存

2. 用做队列

3. 用做集合,也就是存储一批数据的池子。用作有序集合

## 经常遇到的问题

过期之间只能指定到键级别,而不能指定到集合的键级别

## pipeline

imporve performance by combining multi command into one and reduce TCP times

“`
>>> p = r.pipeline() # 创建一个管道
>>> p.set(‘hello’,’redis’)
>>> p.sadd(‘faz’,’baz’)
>>> p.incr(‘num’)
>>> p.execute()
[True, 1, 1]
>>> r.get(‘hello’)
‘redis’

or

>>> p.set(‘hello’,’redis’).sadd(‘faz’,’baz’).incr(‘num’).execute()
“`

默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。

## 坑

python redis 客户端中没有decrby指令

# key 的命名

colon sign : is a convention when naming keys. Try to stick with a schema. For instance “object-type:id:field” can be a nice idea, like in “user:1000:password”. I like to use dots for multi-words fields, like in “comment:1234:reply.to”.

## 使用方法

Redis 是个好东西,提供了很多好用的功能,而且大部分实现的都还既可靠又高效(主从复制除外)。所以一开始我们犯了一个天真的用法错误:把所有不同类型的数据都放在了一组 Redis 集群中。

– 长生命周期的用户状态数据
– 临时缓存数据
– 后台统计用的流水数据

导致的问题就是当你想扩分片的时候,客户端 Hash 映射就变了,这是要迁移数据的。而所有数据放在一组 Redis 里,要把它们分开就麻烦了,每个 Redis 实例里面都是千万级的 key。

根据数据性质把 Redis 集群分类;我的经验是分三类:cache、buffer 和 db

– cache:临时缓存数据,加分片扩容容易,一般无持久化需要。
– buffer:用作缓冲区,平滑后端数据库的写操作,根据数据重要性可能有持久化需求。
– db:替代数据库的用法,有持久化需求。

规避在单实例上放热点 key。

同一系统下的不同子应用或服务使用的 Redis 也要隔离开

linuxbrew

It can be installed in your home directory and does not require root access.

ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install)”
PATH=”$HOME/.linuxbrew/bin:$PATH”

caputre iOS traffic

RVI

Step 1. Plug you iPhone with USB
Step 2. go to pgyer.cn, and find the UDID
Step 3. use rvi -x UDID
Step 4. use wireshark and select the rvi0 device

http://useyourloaf.com/blog/remote-packet-capture-for-ios-devices/

React

Most important ideas:

Widget based

Note: You can write your whole app in React, but you can also use it as a component in your app
props

state

Single data flow with dom-diff

Most Notable methods

React.Component
React.Component.render
React.render

Yifei’s Notes

过去写界面的方法是,把所有的结构(html),动作(js),样式(css)分开,好处是非侵入,离了谁都鞥工作,缺点是无法模块化,在 js 无足轻重,甚至有 noscript 这种插件的过去显然是最佳实践,但是到了 js 大行其道的今天显然模块化又被提出来了。。

EcmaScript 6

## using es6 with babel

### debugging mode

“`


“`

### production mode

`babel script.js –out-file script-compiled.js`

## Yifei’s Notes
the main impovements of ES6 are loops and generators, let/const, arrow functions, class syntax
small pieces are function arguments, destructuring

## Looping

there are 3 ways to loop a sequence in ES5, but there are problems

### ES5 Loops

// not concise
for (var index = 0; index < myArray.length; index++) { console.log(myArray[index]); } // no break or return myArray.forEach(function (value) { console.log(value); }); // for objects, not arrays for (var index in myArray) { // don't actually do this console.log(myArray[index]); } ### Introducing ES6 Loops // concise and correct for (let value of myArray) { console.log(value); } // also works on strings, sets and maps for (let chr of "") { alert(chr); } // make a set from an array of words var uniqueWords = new Set(words); for (let word of uniqueWords) { console.log(word); } for (var [key, value] of phoneBookMap) { console.log(key + "'s phone number is: " + value); } // you can even make it work with objects // dump an object's own enumerable properties to the console for (var key of Object.keys(someObject)) { console.log(key + ": " + someObject[key]); } ## Generator Inside a generator-function, yield is a keyword, with syntax rather like return. The difference is that while a function (even a generator-function) can only return once, a generator-function can yield any number of times. The yield expression suspends execution of the generator so it can be resumed again later. Generator functions are basically the pause-and-continue-able function. when you call a generator function, it returns an paused Generator object, which has a next() function, each time you call the next() function, a pair of yielded-value and status is returned. In technical terms, each time a generator yields, its stack frame—the local variables, arguments, temporary values, and the current position of execution within the generator body—is removed from the stack. However, the Generator object keeps a reference to (or copy of) this stack frame, so that a later .next() call can reactivate it and continue execution. function* fibs() { var a = 0; var b = 1; while (true) { yield a; [a, b] = [b, a + b]; } } function* range(start, stop) { for (var i = start; i < stop; i++) yield i; } // This should "ding" three times for (var value of range(0, 3)) { alert("Ding! at floor #" + value); } This is possible because generators are iterators. All generators have a built-in implementation of .next() and [Symbol.iterator](). ## Template Strings `Hello ${user.name}, welcome to our server for the ${times} times` ## Rest Parameters and Defaults ES5 version ``` function containsAll(haystack) { for (var i = 1; i < arguments.length; i++) { var needle = arguments[i]; if (haystack.indexOf(needle) === -1) { return false; } } return true; } ``` ES6 version ``` function containsAll(haystack, ...needles) { for (var needle of needles) { if (haystack.indexOf(needle) === -1) { return false; } } return true; } ``` ES6 supports default parameters, The default argument gets evaluated at call time, so unlike e.g. in Python, a new object is created each time the function is called. ## Class ES6 support static method, supuer, getter/setter you can even subclass builtin types class Circle { constructor(radius) { this.radius = radius; Circle.circlesMade++; }; static draw(circle, canvas) { // Canvas drawing code }; static get circlesMade() { return !this._count ? 0 : this._count; }; static set circlesMade(val) { this._count = val; }; area() { return Math.pow(this.radius, 2) * Math.PI; }; get radius() { return this._radius; }; set radius(radius) { if (!Number.isInteger(radius)) throw new Error("Circle radius must be an integer."); this._radius = radius; }; } var [,,third] = ["foo", "bar", "baz"]; var [head, ...tail] = [1, 2, 3, 4]; console.log(tail); // [2, 3, 4] var robotA = { name: "Bender" }; var robotB = { name: "Flexo" }; var { name: nameA } = robotA; var { name: nameB } = robotB; console.log(nameA); // "Bender" console.log(nameB); // "Flexo" // this is a syntax sugar for variable and key share the same name var { foo, bar } = { foo: "lorem", bar: "ipsum" }; console.log(foo); // "lorem" console.log(bar); // "ipsum" var [missing = true] = []; console.log(missing); // true var { message: msg = "Something went wrong" } = {}; console.log(msg); // "Something went wrong" var { x = 3 } = {}; console.log(x); // 3 // parameters function removeBreakpoint({ url, line, column }) { // ... } // super works as expected, calling super constructor and access base properties // you can even subclass builtin types CommonJS There is a special object called module.exports, when `require`ing, the value of module.exports is returned. something like that... var require = function(path) { // ... return module.exports; }; ES6 export use the export keyword // lib.js export function foo() {} export class bar {} // or export {baz, foz}; export {foo as fart}; // use.js import {foo, bar} from "lib.js"; import {foo as fart} from "lib.js"; // renaming import {* as lib} from "lib.js"; // import everything and put in a object ES6 import commonJS most packages are written in commonJS, for using as ES6 modules: import _ from "lodash" // which is import {default as _} from "lodash" // which is let _ = require("lodash"); you can also do module.exports in ES6 export default value;

Java Notes

内部类
OuterClassName.InnerClassName
内部类可以直接访问其外围对象的成员,而不需要任何特殊条件
OuterClass.this OuterClass.new

匿名内部类非常有用。 new ClassName() {} 创建一个继承自ClassName的匿名类的对象

嵌套类是指使用了static class 的内部类

java中不允许变量隐藏

只有类成员才会默认初始化,普通变量不会

int x = a.f()
其中,消息是f(), 对象是a,面向对象就是「给对象发消息」

java中不允许将其他类型用做bool值

java会为你提供默认构造器(无参构造器)

无法通过返回值来重载函数

静态块

static {
i = 47;
}

可变参数列表
doGET(Object… args)

默认访问权限是包访问权限

.java 文件编译生成 .class 文件,jar文件是.class 文件的打包

使用import时,默认去CLASSPATH 查找对应的包

enum

enum Color {
GREEN,
RED,
BLUE
}

annotations

@Override
@Deprecated
@SurpressWarnings

Concurrency

class MyRunnable implements Runnable {}
Thread t = new Thread(new Runnable())

AtomicInteger AtomicLong AtomicReference

Oracle Java is more stable than OpenJDK, they are baisclly the same, you can think oracle java as a bugfix version of OpenJDK

资料

learnxinyminutes
Java for c++ programmers http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/
Effecitve Java

坑1

每个文件一个public类, 要求类的名称必须和文件名一样
当然还可以定义一些其他的非 public 的类
javac MyClass.java 生成 MyClass.class 等其他一些文件, 每个类对应一个 class 文件
java MyClass // 这里没有 class 后缀

坑2

数组 int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // 未指定元素会被初始化
ArrayList
LinkedList

.length 属性
for 语句
for (int i : numbers) {
//
}

使用 System.arraycopy 来复制数组

坑3

switch语句
java 中的 switch 语句和 c 一样, 依然是 fall-through, 在 java 7 中可以 switch string 了

坑4

类型转换: Integer.parseInt, Interger.toString

坑5

static block
Java has no implementation of static constructors, but has a static block that can be used to initialize class variables (static variables).
This block will be called when the class is loaded.

static {
className = “Bicycle”;
}

坑6

构造器可以调用另一个构造器, 使用 this, 还可以使用 super 调用父类的构造器
java 只能单继承, 但是可以实现多个接口, 接口中的方法不能有定义

坑7

不能使用==来判断对象(包括字符串)是否相等, 这样只能检测是否指向同一位置.

坑8 – IO

System.out.println

坑9 – 类型

原始类型 数字型
引用类型 数组和类

所有类都是 Object 的子类

坑10 – Java.lang

String 字符串类
length 长度
equals 相等
toLowerCase/toUpperCase

包装类
Integer
Boolean
Double

坑11 – Package

使用 package name; 来声明包, 所有的源文件要放在 name 这个文件夹下
可以使用 import package.class 或者 import package.* 来导入包
CLASSPATH 存放 package 的根目录

坑12 – 异常

try {
// statements that might cause exceptions
// possibly including function calls
} catch ( exception-1 id-1 ) {
// statements to handle this exception
} catch ( exception-2 id-2 ) {
// statements to handle this exception
.
.
.
} finally {
// statements to execute every time this try block executes
}

try {
return 0; // 被覆盖
} finally {
return 2;
}
尽量避免在 try…catch…finally 中使用控制转移语句

+——–+
| Object |
+——–+
|
|
+———–+
| Throwable |
+———–+
/ \
/ \
+——-+ +———–+
| Error | | Exception |
+——-+ +———–+
/ | \ / | \
\________/ \______/ \
+——————+
unchecked checked | RuntimeException |
+——————+
/ | | \
\_________________/

unchecked

checked 和 unchecked exception, RuntimeException 通常是指 unchecked exception
实际上,对于自己编写的异常类来讲,推荐默认的是继承RuntimeException,除非有特殊理由才继承Exception。 C#中没有Checked Exception的概念,这种推荐的做法等于是采用了C#的设计理念:把是否捕获和何时捕获这个问题交给使用者决定,不强制使用者。当然,如果某些情况 下明确提醒捕获更加重要还是可以采用Checked Exception的。对于编写一个方法来讲,“是否在方法上声明一个异常”这个问题比“是否采用Checked Exception”更加重要。

坑13 – OO
Each superclass method (except its constructors) can be either
• inherited, or
• overloaded, or
• overridden
inherited: If no method with the same name is (re)defined in the subclass, then the subclass has that method with the same implementation as in the superclass.
overloaded: If the subclass defines a method with the same name, but with a different number of arguments or different argument types, then the subclass has two methods with that name: the old one defined by the superclass, and the new one it defined.
overridden: If the subclass defines a method with the same name, and the same number and types of arguments, then the subclass has only one method with that name: the new one it defined.
使用 super 调用基类的构造器,使用 this 调用自身其他的构造器,如果没有调用基类的构造器,会隐式调用基类的默认构造器

Recall that every class extends Object. So you might wonder which methods of Object you should consider overriding when you define a new class. There are four methods that often should be overridden:
1. toString
2. equals
3. hashCode
4. clone
public String toString(): Returns a String representation of the object. It is used, for example, by System.out.print to print an object. The default version of toString is not very useful, so you should override this method whenever you want to provide a String representation of your class objects.
public boolean equals(Object ob): Returns true iff the object (pointed to by “this”) and ob are the same. The default version uses pointer equality; i.e., it returns true only if “this” and “ob” contain the same address. You may want to override this method to provide a more liberal notion of equality. For example, the String class overrides equals so that it returns true for two Strings that contain the same sequence of characters.
public int hashCode(): Returns an integer for this object suitable for use as a hash code (e.g., for use with the Hashtable class defined in javil.util). This method should be overridden whenever the equals method is, so that hashCode returns the same value for two “equal” objects.
protected Object clone(): Returns a copy of this object (note that no constructor is called for the new object). The default version just copies the values of all fields (i.e., a “shallow” copy). That is probably not what you want when your class has fields that contain pointers (i.e., arrays or classes). So in that case you should override the clone method to do a deep copy — clone all pointer fields.
Cloning
To permit your object to be cloned you must declare that your object implements the Cloneable interface. (See the notes on INTERFACES.) For example:
public class List implements Cloneable {
private Object items[]; // a pointer field!

}
If you forget to do this, an attempt to clone will cause the exception CloneNotSupportedException to be thrown.

坑14 – casting
java 需要显式转换类型
Assume that we have the following declarations of function f and variables h1 and h2:
public static void f( RaceHorse r ) { … }
Horse h1 = new RaceHorse();
Horse h2 = new Horse();
Now consider the following three calls to f:
1. f(h1); // compile-time error (missing cast)
2. f( (RaceHorse)h1 ); // fine! h1 really does point to a RaceHorse
3. f( (RaceHorse)h2 ); // runtime error (bad cast) h2 points to a Horse

静态方法和成员变量并没有 dynamic dispatching
可以使用 super.xxx()调用父类已经被重载的方法

坑15 – Interface

lua

lua_pcall 是使用 c 中的 setjmp 实现的, 对应在lua 中的函数就是 pcall

pcall/error 大概就相当于其他语言中的 try-catch /throw了

“`
local ok, errorobject = pcall(function()
    –here goes the protected code
    …
end)

if not ok then
    –here goes the error handling code
    –(errorobject has more information about the error)
    …
end
“`

# 协程

对称协程只有一个关键字: transfer, 类似于 goto 语句, 把控制权移交给其他的任意一个协程; 而非对称协程一般有两个关键字:resume 和 yield, 使用 resume打开一个协程, 然后在这个协程中使用 yield 返回.

学习lua可以获得

* 怎样实现一门语言,编译原理,离散数学
* lua本身
* 虚拟机,jit
* C语言能力的增强

lua 的标准库补充 [Penlight](https://github.com/stevedonovan/Penlight)

![](http://ww4.sinaimg.cn/large/006tNbRwgy1fg3q43bjn9j31kw1rgk1j.jpg)