From 0b271b5746c5491c6a20d0e99d5691bab4e147b3 Mon Sep 17 00:00:00 2001 From: Benny Date: Thu, 11 Jan 2018 10:43:32 +0800 Subject: [PATCH 01/25] Update python.wiki --- libs/python.wiki | 174 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/libs/python.wiki b/libs/python.wiki index 8e7a667..fe8e84c 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -424,6 +424,50 @@ Home:[https://pypi.python.org/pypi/setuptools] 这套工具可以帮助你进行第三方库的管理(下载、编译、安装、升级、卸载) + +== 3.8 系统管理 == + +

sys

+【标准库】 + +这个模块可供访问由解释器使用或维护的变量和与解释器进行交互的函数。 + +代码示例 + +sys.argv # 命令行参数List,第一个元素是程序本身路径 +sys.exit(n) # 退出程序,正常退出时exit(0) +sys.version # 获取Python解释程序的版本信息 + + +

platform

+【标准库】 + +这个模块提供了很多用于获取操作系统的信息的功能。 + +代码示例 + +import platform + +platform.platform() # 获取操作系统名称及版本号,'Windows-7-6.1.7601-SP1' +platform.version() # 获取操作系统版本号,'6.1.7601' +platform.architecture() # 获取操作系统的位数,('32bit', 'WindowsPE') + + +

psutil

+ +Home:[https://github.com/giampaolo/psutil] + +psutil(Python system and process utilities)是一个跨平台的进程管理和系统工具的python库,可以处理系统CPU,memory,disks,network等信息。主要用于系统资源的监控,分析,以及对进程进行一定的管理。 + +代码示例 + +import psutil + +psutil.cpu_count() # 获取CPU数量 +psutil.cpu_freq()   # 获取CPU频率 +psutil.virtual_memory()     # 获取内存信息 + + ---- = 4 Web = @@ -561,6 +605,36 @@ if __name__ == "__main__" : app.run() +

Flask

+ +Home:[http://flask.pocoo.org/] + +Links:[https://zh.wikipedia.org/wiki/Flask] + +轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2 模板引擎。 + +代码示例——Hello world + +from flask import Flask +app = Flask(__name__) + +@app.route('/') +def hello_world(): + return 'Hello World!' + +if __name__ == '__main__': + app.run() + + + +

Tornado

+ +Home:[http://www.tornadoweb.org/en/stable/] + +Links:[https://zh.wikipedia.org/wiki/Tornado] + +同样也是轻量级的Web框架,类似Web.py,但是异步非阻塞IO的处理方式使其拥有较为出色的抗负载能力。 + == 4.4 Web前端 & JS整合 ==

Pyjamas & pyjs

@@ -632,6 +706,34 @@ Home:[https://github.com/google/pywebsocket] 该项目包含一个可独立运行的 server 以及一个 Apache 扩展模块(mod_pywebsocket)。 +

selenium

+ +Home:[http://www.seleniumhq.org/] + +selenium是一个非常优秀的用于爬虫、Web自动化测试测试的框架。 + +代码示例(模拟登录) + + +from selenium import webdriver + +driver = webdriver.Chrome() + +driver.get('http://192.168.1.1') +driver.find_element_by_xpath('//*[@id="lgPwd"]').send_keys('123456') +driver.find_element_by_xpath('//*[@id="loginSub"]').click() +driver.quit() + + + +

scrapy

+ +Home:[https://scrapy.org/] + +Links:[https://en.wikipedia.org/wiki/Scrapy] +Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 + + ---- = 5 网络 = @@ -761,6 +863,21 @@ with pysftp.Connection("hostxxx", username="userxxx", password="xxxxxx") as sftp 封装 POP3(Post Office Protocol v3)协议 +

yagmail

+ +Home:[https://github.com/kootenpv/yagmail] + +一个非常简单易用的用来发送邮件的第三方库。 + +代码示例 + +import yagmail +yag = yagmail.SMTP() +contents = ['This is the body, and here is just text http://somedomain/image.png', + 'You can find an audio file attached.', '/local/path/song.mp3'] +yag.send('to@someone.com', 'subject', contents) + + === 5.3.5 即时通讯 ===

jabber.py

@@ -813,6 +930,22 @@ Home:[https://github.com/citronneur/rdpy] 纯 Python 实现的 RDP([https://en.wikipedia.org/wiki/Remote_Desktop_Protocol 微软远程桌面协议])和 VNC([https://en.wikipedia.org/wiki/Virtual_Network_Computing Virtual Network Computing])客户端,依赖于 Twisted 库 +

paramiko

+ +Home:[http://www.paramiko.org/] + +paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。除了SSH协议之外,paramiko还支持SFTP。 + +代码示例 + +import paramiko +ssh = paramiko.SSHClient() + +ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +ssh.connect("IP", port, "username", "password") + + + === 5.3.7 (其它) ===

urlparse

@@ -1525,6 +1658,13 @@ Home:[http://code.enthought.com/chaco/] 这是一个商业公司维护的库,主要提供2维图表。效果图在“[http://docs.enthought.com/chaco/user_manual/annotated_examples.html 这里]”。 + +

Plotly

+ +Home:[https://plot.ly/d3-js-for-python-and-pandas-charts/] + +plotly是现代平台的敏捷商业智能和数据科学库,它作为一款开源的绘图库,可以应用于Python、R、MATLAB、Excel、JavaScript和jupyter等多种语言 + ---- = 8 信息安全 = @@ -1567,6 +1707,31 @@ Home:[https://github.com/google/keyczar] 它提供了比较高层的 API, 使用者无需关心太多的细节。 + +

passlib

+ +Home:[https://passlib.readthedocs.io/en/stable/index.html] + +passlib是一个久经考验的非常成熟的跨平台的散列函数库,他所提供的功能包括随机盐密码的生成与验证,两步验证等。 + +代码示例——验证随机盐密码 + +>>> # import the hash algorithm +>>> from passlib.hash import pbkdf2_sha256 + +>>> # generate new salt, and hash a password +>>> hash = pbkdf2_sha256.hash("toomanysecrets") +>>> hash +'$pbkdf2-sha256$29000$N2YMIWQsBWBMae09x1jrPQ$1t8iyB2A.WF/Z5JZv.lfCIhXXN33N23OSgQYThBYRfk' + +>>> # verifying the password +>>> pbkdf2_sha256.verify("toomanysecrets", hash) +True +>>> pbkdf2_sha256.verify("joshua", hash) +False + + + == 8.2 访问控制 ==

oauth2client

@@ -1719,6 +1884,15 @@ Home:[http://lxml.de/] 以回调方式解析 HTML/XHTML 文件内容。 +

beautifulsoup

+ +Home:[https://www.crummy.com/software/BeautifulSoup/bs4/doc/] + +Links:[https://zh.wikipedia.org/zh-cn/Beautiful_Soup] + +Beautiful Soup 是一个可以从HTML 或XML 文件中提取数据的Python 库,爬虫利器,通常与requests或者selenium配合。 + + == 9.4 PDF ==

pyfpdf

From 0eb16637748810d95845d4b060093fa6c221573d Mon Sep 17 00:00:00 2001 From: Benny Date: Thu, 11 Jan 2018 10:45:49 +0800 Subject: [PATCH 02/25] Update python.wiki --- libs/python.wiki | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index fe8e84c..7a22d73 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -712,7 +712,7 @@ Home:[http://www.seleniumhq.org/] selenium是一个非常优秀的用于爬虫、Web自动化测试测试的框架。 -代码示例(模拟登录) +代码示例——模拟登录 from selenium import webdriver @@ -731,6 +731,7 @@ driver.quit() Home:[https://scrapy.org/] Links:[https://en.wikipedia.org/wiki/Scrapy] + Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 From ddceeb343810b94ca2b03ad37094a15d693ae0e1 Mon Sep 17 00:00:00 2001 From: Benny Date: Thu, 11 Jan 2018 10:58:38 +0800 Subject: [PATCH 03/25] =?UTF-8?q?sys,=20platform,=20psutil,=20flask,=20tor?= =?UTF-8?q?nado,=20selenium=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sys, platform, psutil, flask, tornado, selenium, scrapy, yagmail, paramiko, plotly, passlib, beautifulsoup, telegram --- libs/python.wiki | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index 7a22d73..651fe4d 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -893,6 +893,19 @@ Home:[https://bitbucket.org/jaraco/irc] IRC 是 Internet Relay Chat 的缩写。这是用 Python 封装的第三方库。 +

pyTelegramBotAPI

+ +Home:[https://github.com/eternnoir/pyTelegramBotAPI] + +一个简单、易用的 [https://core.telegram.org/bots TelegramBot] 封装。 + +

Telethon

+ +Home:[https://github.com/LonamiWebs/Telethon] + +纯 Python 3 的 [https://telegram.org/ Telegram] 客户端封装。 + + === 5.3.6 远程控制 ===

telnetlib

@@ -1713,7 +1726,7 @@ Home:[https://github.com/google/keyczar] Home:[https://passlib.readthedocs.io/en/stable/index.html] -passlib是一个久经考验的非常成熟的跨平台的散列函数库,他所提供的功能包括随机盐密码的生成与验证,两步验证等。 +passlib是一个久经考验的非常成熟的跨平台的散列函数库,它所提供的功能包括随机盐密码的生成与验证,两步验证等。 代码示例——验证随机盐密码 From d1044390a9a0df4a47a32d97ccc0d82b1d4c350b Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 22:10:57 +0800 Subject: [PATCH 04/25] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=AD=E7=9A=84=20HTML=20=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=EF=BC=88=E8=B2=8C=E4=BC=BC=E4=B8=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E4=BA=86=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 116 ++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 63 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index 651fe4d..825145a 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -296,7 +296,7 @@ threading.Thread(target=my_thread).start() import threading import time -from __future__ import print_function +from __future__ import print_function class MyThread(threading.Thread) : def run(self) : @@ -304,7 +304,7 @@ class MyThread(threading.Thread) : time.sleep(3) print("{} finished!".format(self.getName())) -if __name__ == "__main__" : +if __name__ == "__main__" : for n in range(10) : mythread = MyThread(name = "Thread-{}".format(n + 1)) mythread.start() @@ -322,8 +322,8 @@ if __name__ == "__main__" : 代码示例——启动命令行进程,并获取该进程的标准输出 import subprocess -output = subprocess.check_output(["dir"]) # 获取当前目录的内容 -output = subprocess.check_output(["netstat", "-an"]) # 获取当前网络链接 +output = subprocess.check_output(["dir"]) # 获取当前目录的内容 +output = subprocess.check_output(["netstat", "-an"]) # 获取当前网络链接

multiprocessing

@@ -343,7 +343,7 @@ def f(lock, n) : print("hello world %d" % n) lock.release() -if __name__ == "__main__" : +if __name__ == "__main__" : lock = Lock() for num in range(10): Process(target=f, args=(lock, num)).start() @@ -366,7 +366,7 @@ map = mmap.mmap(-1, 13) map.write("Hello, world") pid = os.fork() -if pid == 0 : # 子进程 +if pid == 0 : # 子进程 map.seek(0) print(map.readline()) map.close() @@ -424,10 +424,10 @@ Home:[https://pypi.python.org/pypi/setuptools] 这套工具可以帮助你进行第三方库的管理(下载、编译、安装、升级、卸载) - == 3.8 系统管理 ==

sys

+ 【标准库】 这个模块可供访问由解释器使用或维护的变量和与解释器进行交互的函数。 @@ -440,6 +440,7 @@ sys.version # 获取Python解释程序的版本信息

platform

+ 【标准库】 这个模块提供了很多用于获取操作系统的信息的功能。 @@ -600,7 +601,7 @@ class index: def GET(self): return "Hello, world!" -if __name__ == "__main__" : +if __name__ == "__main__" : app = web.application(urls, globals()) app.run() @@ -609,7 +610,7 @@ if __name__ == "__main__" : Home:[http://flask.pocoo.org/] -Links:[https://zh.wikipedia.org/wiki/Flask] +Links:[https://zh.wikipedia.org/wiki/Flask 维基百科] 轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2 模板引擎。 @@ -624,14 +625,13 @@ def hello_world(): if __name__ == '__main__': app.run() -

Tornado

Home:[http://www.tornadoweb.org/en/stable/] -Links:[https://zh.wikipedia.org/wiki/Tornado] +Links:[https://zh.wikipedia.org/wiki/Tornado 维基百科] 同样也是轻量级的Web框架,类似Web.py,但是异步非阻塞IO的处理方式使其拥有较为出色的抗负载能力。 @@ -677,15 +677,15 @@ import PyV8 ctxt1 = PyV8.JSContext() ctxt1.enter() -ctxt1.eval("1+2") # 对 JS 表达式求值 +ctxt1.eval("1+2") # 对 JS 表达式求值 -class Global(PyV8.JSClass) : # 定义一个兼容 JS 的类 +class Global(PyV8.JSClass) : # 定义一个兼容 JS 的类 def hello(self) : print("Hello, world") -ctxt2 = PyV8.JSContext(Global()) # 创建一个 JS 上下文,传入 Global 类的对象 +ctxt2 = PyV8.JSContext(Global()) # 创建一个 JS 上下文,传入 Global 类的对象 ctxt2.enter() -ctxt2.eval("hello()") # 调用 hello() 函数 +ctxt2.eval("hello()") # 调用 hello() 函数

PyWebKitGtk

@@ -714,27 +714,23 @@ selenium是一个非常优秀的用于爬虫、Web自动化测试测试的框架 代码示例——模拟登录 - from selenium import webdriver driver = webdriver.Chrome() - driver.get('http://192.168.1.1') driver.find_element_by_xpath('//*[@id="lgPwd"]').send_keys('123456') driver.find_element_by_xpath('//*[@id="loginSub"]').click() driver.quit() -

scrapy

Home:[https://scrapy.org/] -Links:[https://en.wikipedia.org/wiki/Scrapy] +Links:[https://en.wikipedia.org/wiki/Scrapy Wikipedia] Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 - ---- = 5 网络 = @@ -753,10 +749,10 @@ Links:[https://en.wikipedia.org/wiki/Scapy Wikipedia] 代码示例 -# 传统的 ping 扫描(网络层) +# 传统的 ping 扫描(网络层) ans,unans = sr(IP(dst="192.168.1.1-254")/ICMP()) -# 局域网内的 ARP 扫描(链路层) +# 局域网内的 ARP 扫描(链路层) ans,unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst="192.168.1.0/24"), timeout=2) @@ -821,10 +817,10 @@ body = buffer.getvalue() from ftplib import FTP -ftp = FTP("ftp.debian.org") # 连接服务器(如果不指定端口号,则用默认端口号 21) -ftp.login() # 登录(如果不指定用户名和密码,则用匿名登录) -ftp.cwd("debian") # 切换到 "debian" 目录 -ftp.retrlines("LIST") # 列出当前目录的内容 +ftp = FTP("ftp.debian.org") # 连接服务器(如果不指定端口号,则用默认端口号 21) +ftp.login() # 登录(如果不指定用户名和密码,则用匿名登录) +ftp.cwd("debian") # 切换到 "debian" 目录 +ftp.retrlines("LIST") # 列出当前目录的内容 ftp.quit() @@ -839,9 +835,9 @@ Home:[https://bitbucket.org/dundeemt/pysftp] import pysftp with pysftp.Connection("hostxxx", username="userxxx", password="xxxxxx") as sftp : - with sftp.cd("public") # 服务端当前目录切换到 public - sftp.put("/my/local/filename") # 上传某个本地文件到服务端的 public 目录 - sftp.get_r("myfiles", "/local") # 递归复制某个服务端的目录到本地 + with sftp.cd("public") # 服务端当前目录切换到 public + sftp.put("/my/local/filename") # 上传某个本地文件到服务端的 public 目录 + sftp.get_r("myfiles", "/local") # 递归复制某个服务端的目录到本地 === 5.3.4 电子邮件 === @@ -905,7 +901,6 @@ Home:[https://github.com/LonamiWebs/Telethon] 纯 Python 3 的 [https://telegram.org/ Telegram] 客户端封装。 - === 5.3.6 远程控制 ===

telnetlib

@@ -959,7 +954,6 @@ ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect("IP", port, "username", "password") - === 5.3.7 (其它) ===

urlparse

@@ -1057,7 +1051,7 @@ Home:[https://github.com/tonysimpson/nanomsg-python] 代码示例——Hello world -from __future__ import print_function +from __future__ import print_function from nanomsg import Socket, PAIR, PUB s1 = Socket(PAIR) @@ -1230,7 +1224,7 @@ sqlite3 从 Python 2.5 版本开始加入到标准库中。通过它,你可以 代码示例——创建一个内存数据库,建表并插入记录 import sqlite3 -conn = sqlite3.connect(":memory:") # ":memory:" 表示这是一个内存数据库 +conn = sqlite3.connect(":memory:") # ":memory:" 表示这是一个内存数据库 cursor = conn.cursor() cursor.execute("CREATE TABLE person (name text, age int)") cursor.execute("INSERT INTO stocks VALUES ('TOM',20)") @@ -1324,7 +1318,7 @@ from sqlalchemy.orm import relation, sessionmaker Base = declarative_base() class Movie(Base) : - __tablename__ = "movies" + __tablename__ = "movies" id = Column(Integer, primary_key=True) title = Column(String(255), nullable=False) @@ -1332,23 +1326,23 @@ class Movie(Base) : directed_by = Column(Integer, ForeignKey("directors.id")) director = relation("Director", backref="movies", lazy=False) - def __init__(self, title=None, year=None) : + def __init__(self, title=None, year=None) : self.title = title self.year = year - def __repr__(self) : + def __repr__(self) : return "Movie(%r, %r, %r)" % (self.title, self.year, self.director) class Director(Base) : - __tablename__ = "directors" + __tablename__ = "directors" id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False, unique=True) - def __init__(self, name=None) : + def __init__(self, name=None) : self.name = name - def __repr__(self) : + def __repr__(self) : return "Director(%r)" % (self.name) Base.metadata.create_all(create_engine("dbms://user:pwd@host/dbname")) @@ -1393,14 +1387,14 @@ class Person(Model) : birthday = DateField() is_relative = BooleanField() class Meta : - database = db # This model uses the "test.db". + database = db # This model uses the "test.db". class Pet(Model) : owner = ForeignKeyField(Person, related_name="pets") name = CharField() animal_type = CharField() class Meta : - database = db # This model uses the "test.db". + database = db # This model uses the "test.db". db.connect() db.create_tables([Person, Pet]) @@ -1428,7 +1422,7 @@ Python2 的模块名叫 Tkinter,到 Python3 模块名改为 tkinter from Tkinter import * -if __name__ == "__main__" : +if __name__ == "__main__" : root = Tk() label = Label(root, text="Hello, world") label.pack() @@ -1462,7 +1456,7 @@ class App(wx.App) : self.SetTopWindow(self.frame) return True -if __name__ == "__main__" : +if __name__ == "__main__" : app = App() app.MainLoop() @@ -1491,7 +1485,7 @@ pygtk.require("2.0") import gtk class HelloWorld : - def __init__(self) : + def __init__(self) : self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect("delete_event", self.delete_event) self.window.connect("destroy", self.destroy) @@ -1518,7 +1512,7 @@ class HelloWorld : def destroy(self, widget, data=None) : gtk.main_quit() -if __name__ == "__main__" : +if __name__ == "__main__" : hello = HelloWorld() hello.main() @@ -1534,8 +1528,8 @@ Home:[https://live.gnome.org/PyGObject] from gi.repository import Gtk class MyWindow(Gtk.Window): - def __init__(self): - Gtk.Window.__init__(self, title="Hello World") + def __init__(self): + Gtk.Window.__init__(self, title="Hello World") self.button = Gtk.Button(label="Click Here") self.button.connect("clicked", self.on_button_clicked) @@ -1567,7 +1561,7 @@ Links:[https://en.wikipedia.org/wiki/PyQt Wikipedia] [https://zh.wikipedia.org import sys from PyQt4.QtGui import * -if __name__ == "__main__" : +if __name__ == "__main__" : app = QApplication(sys.argv) window = QWidget() @@ -1672,7 +1666,6 @@ Home:[http://code.enthought.com/chaco/] 这是一个商业公司维护的库,主要提供2维图表。效果图在“[http://docs.enthought.com/chaco/user_manual/annotated_examples.html 这里]”。 -

Plotly

Home:[https://plot.ly/d3-js-for-python-and-pandas-charts/] @@ -1721,7 +1714,6 @@ Home:[https://github.com/google/keyczar] 它提供了比较高层的 API, 使用者无需关心太多的细节。 -

passlib

Home:[https://passlib.readthedocs.io/en/stable/index.html] @@ -1745,7 +1737,6 @@ True False - == 8.2 访问控制 ==

oauth2client

@@ -1783,14 +1774,14 @@ JSON 格式源自 JavaScript,如今在 Web 开发中广为应用。 import json json.dumps(["foo", {"bar": ("baz", None, 1.0, 2)}]) -# JSON 编码 -# 得到如下【字符串】 -# """["foo", {"bar": ["baz", null, 1.0, 2]}]""" +# JSON 编码 +# 得到如下【字符串】 +# """["foo", {"bar": ["baz", null, 1.0, 2]}]""" json.loads("""["foo", {"bar":["baz", null, 1.0, 2]}]""") -# JSON 解码 -# 得到如下【对象】 -# [u"foo", {u"bar": [u"baz", None, 1.0, 2]}] +# JSON 解码 +# 得到如下【对象】 +# [u"foo", {u"bar": [u"baz", None, 1.0, 2]}] === 9.1.3 YAML === @@ -1902,11 +1893,10 @@ Home:[http://lxml.de/] Home:[https://www.crummy.com/software/BeautifulSoup/bs4/doc/] -Links:[https://zh.wikipedia.org/zh-cn/Beautiful_Soup] +Links:[https://zh.wikipedia.org/zh-cn/Beautiful_Soup 维基百科] Beautiful Soup 是一个可以从HTML 或XML 文件中提取数据的Python 库,爬虫利器,通常与requests或者selenium配合。 - == 9.4 PDF ==

pyfpdf

@@ -2105,11 +2095,11 @@ Home:[https://github.com/xflr6/graphviz] from graphviz import Digraph dot = Digraph(comment='The Round Table') -# 添加节点 +# 添加节点 dot.node('A', 'King Arthur') dot.node('B', 'Sir Bedevere the Wise') dot.node('L', 'Sir Lancelot the Brave') -# 添加连线 +# 添加连线 dot.edges(['AB', 'AL']) dot.edge('B', 'L', constraint='false') @@ -2196,14 +2186,14 @@ Links:[https://en.wikipedia.org/wiki/NumPy Wikipedia] [https://zh.wikipedia.or 代码示例 -# 以下是传统 Python 写法,冗长且速度较慢 +# 以下是传统 Python 写法,冗长且速度较慢 a = range(10000000) b = range(10000000) c = [] for i in range(len(a)) : c.append(a[i] + b[i]) -# 以下是 NumPy 的写法,简洁且速度飞快 +# 以下是 NumPy 的写法,简洁且速度飞快 import numpy as np a = np.arange(10000000) b = np.arange(10000000) From 330a7d7261217f465c4c3de4fe7b05e1cf88c647 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 22:32:13 +0800 Subject: [PATCH 05/25] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 85 ++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index 825145a..ae59560 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -296,7 +296,7 @@ threading.Thread(target=my_thread).start() import threading import time -from __future__ import print_function +from __future__ import print_function class MyThread(threading.Thread) : def run(self) : @@ -304,7 +304,7 @@ class MyThread(threading.Thread) : time.sleep(3) print("{} finished!".format(self.getName())) -if __name__ == "__main__" : +if __name__ == "__main__" : for n in range(10) : mythread = MyThread(name = "Thread-{}".format(n + 1)) mythread.start() @@ -343,7 +343,7 @@ def f(lock, n) : print("hello world %d" % n) lock.release() -if __name__ == "__main__" : +if __name__ == "__main__" : lock = Lock() for num in range(10): Process(target=f, args=(lock, num)).start() @@ -434,9 +434,9 @@ Home:[https://pypi.python.org/pypi/setuptools] 代码示例 -sys.argv # 命令行参数List,第一个元素是程序本身路径 -sys.exit(n) # 退出程序,正常退出时exit(0) -sys.version # 获取Python解释程序的版本信息 +sys.argv # 命令行参数 List,第一个元素是程序本身路径 +sys.exit(0) # 退出程序,正常退出时用 0 表示退出码 +sys.version # 获取 Python 解释程序的版本信息

platform

@@ -449,9 +449,9 @@ sys.version # 获取Python解释程序的版本信息 import platform -platform.platform() # 获取操作系统名称及版本号,'Windows-7-6.1.7601-SP1' -platform.version() # 获取操作系统版本号,'6.1.7601' -platform.architecture() # 获取操作系统的位数,('32bit', 'WindowsPE') +platform.platform() # 获取操作系统名称及版本号,例如:"Windows-7-6.1.7601-SP1" +platform.version() # 获取操作系统版本号,例如:"6.1.7601" +platform.architecture() # 获取操作系统的架构,例如:("32bit", "WindowsPE")

psutil

@@ -464,9 +464,9 @@ psutil(Python system and process utilities)是一个跨平台的进程管理和 import psutil -psutil.cpu_count() # 获取CPU数量 -psutil.cpu_freq()   # 获取CPU频率 -psutil.virtual_memory()     # 获取内存信息 +psutil.cpu_count() # 获取 CPU 数量 +psutil.cpu_freq() # 获取 CPU 频率 +psutil.virtual_memory() # 获取内存信息 ---- @@ -571,8 +571,8 @@ Links:[https://en.wikipedia.org/wiki/CherryPy Wikipedia] import cherrypy -class HelloWorld(object): - def index(self): +class HelloWorld(object) : + def index(self) : return "Hello World!" index.exposed = True @@ -597,11 +597,11 @@ urls = ( "/", "index" ) -class index: - def GET(self): +class index : + def GET(self) : return "Hello, world!" -if __name__ == "__main__" : +if __name__ == "__main__" : app = web.application(urls, globals()) app.run() @@ -617,13 +617,13 @@ Links:[https://zh.wikipedia.org/wiki/Flask 维基百科] 代码示例——Hello world from flask import Flask -app = Flask(__name__) +app = Flask(__name__) -@app.route('/') -def hello_world(): - return 'Hello World!' +app.route("/") +def hello_world() : + return “Hello World!" -if __name__ == '__main__': +if __name__ == "__main__" : app.run() @@ -717,8 +717,8 @@ selenium是一个非常优秀的用于爬虫、Web自动化测试测试的框架 from selenium import webdriver driver = webdriver.Chrome() -driver.get('http://192.168.1.1') -driver.find_element_by_xpath('//*[@id="lgPwd"]').send_keys('123456') +driver.get("http://192.168.1.1") +driver.find_element_by_xpath('//*[@id="lgPwd"]').send_keys("123456") driver.find_element_by_xpath('//*[@id="loginSub"]').click() driver.quit() @@ -869,10 +869,11 @@ Home:[https://github.com/kootenpv/yagmail] 代码示例 import yagmail + yag = yagmail.SMTP() -contents = ['This is the body, and here is just text http://somedomain/image.png', - 'You can find an audio file attached.', '/local/path/song.mp3'] -yag.send('to@someone.com', 'subject', contents) +contents = ["This is the body, and here is just text http://somedomain/image.png", + "You can find an audio file attached.', '/local/path/song.mp3"] +yag.send("to@someone.com", "subject", contents) === 5.3.5 即时通讯 === @@ -948,8 +949,8 @@ paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加 代码示例 import paramiko -ssh = paramiko.SSHClient() +ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect("IP", port, "username", "password") @@ -1051,7 +1052,7 @@ Home:[https://github.com/tonysimpson/nanomsg-python] 代码示例——Hello world -from __future__ import print_function +from __future__ import print_function from nanomsg import Socket, PAIR, PUB s1 = Socket(PAIR) @@ -1318,7 +1319,7 @@ from sqlalchemy.orm import relation, sessionmaker Base = declarative_base() class Movie(Base) : - __tablename__ = "movies" + __tablename__ = "movies" id = Column(Integer, primary_key=True) title = Column(String(255), nullable=False) @@ -1326,23 +1327,23 @@ class Movie(Base) : directed_by = Column(Integer, ForeignKey("directors.id")) director = relation("Director", backref="movies", lazy=False) - def __init__(self, title=None, year=None) : + def __init__(self, title=None, year=None) : self.title = title self.year = year - def __repr__(self) : + def __repr__(self) : return "Movie(%r, %r, %r)" % (self.title, self.year, self.director) class Director(Base) : - __tablename__ = "directors" + __tablename__ = "directors" id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False, unique=True) - def __init__(self, name=None) : + def __init__(self, name=None) : self.name = name - def __repr__(self) : + def __repr__(self) : return "Director(%r)" % (self.name) Base.metadata.create_all(create_engine("dbms://user:pwd@host/dbname")) @@ -1422,7 +1423,7 @@ Python2 的模块名叫 Tkinter,到 Python3 模块名改为 tkinter from Tkinter import * -if __name__ == "__main__" : +if __name__ == "__main__" : root = Tk() label = Label(root, text="Hello, world") label.pack() @@ -1456,7 +1457,7 @@ class App(wx.App) : self.SetTopWindow(self.frame) return True -if __name__ == "__main__" : +if __name__ == "__main__" : app = App() app.MainLoop() @@ -1485,7 +1486,7 @@ pygtk.require("2.0") import gtk class HelloWorld : - def __init__(self) : + def __init__(self) : self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect("delete_event", self.delete_event) self.window.connect("destroy", self.destroy) @@ -1512,7 +1513,7 @@ class HelloWorld : def destroy(self, widget, data=None) : gtk.main_quit() -if __name__ == "__main__" : +if __name__ == "__main__" : hello = HelloWorld() hello.main() @@ -1528,8 +1529,8 @@ Home:[https://live.gnome.org/PyGObject] from gi.repository import Gtk class MyWindow(Gtk.Window): - def __init__(self): - Gtk.Window.__init__(self, title="Hello World") + def __init__(self): + Gtk.Window.__init__(self, title="Hello World") self.button = Gtk.Button(label="Click Here") self.button.connect("clicked", self.on_button_clicked) @@ -1561,7 +1562,7 @@ Links:[https://en.wikipedia.org/wiki/PyQt Wikipedia] [https://zh.wikipedia.org import sys from PyQt4.QtGui import * -if __name__ == "__main__" : +if __name__ == "__main__" : app = QApplication(sys.argv) window = QWidget() From 496dfe4c1899a7de5b62db5fa3a4bec44f7807fe Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 22:41:11 +0800 Subject: [PATCH 06/25] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=8B=A5=E5=B9=B2?= =?UTF-8?q?=E5=BA=93=E7=9A=84=20home=20=E7=BD=91=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index ae59560..a4703d7 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -629,7 +629,7 @@ if __name__ == "__main__" :

Tornado

-Home:[http://www.tornadoweb.org/en/stable/] +Home:[http://www.tornadoweb.org/] Links:[https://zh.wikipedia.org/wiki/Tornado 维基百科] @@ -1669,7 +1669,7 @@ Home:[http://code.enthought.com/chaco/]

Plotly

-Home:[https://plot.ly/d3-js-for-python-and-pandas-charts/] +Home:[https://plot.ly/] plotly是现代平台的敏捷商业智能和数据科学库,它作为一款开源的绘图库,可以应用于Python、R、MATLAB、Excel、JavaScript和jupyter等多种语言 @@ -1717,7 +1717,7 @@ Home:[https://github.com/google/keyczar]

passlib

-Home:[https://passlib.readthedocs.io/en/stable/index.html] +Home:[https://bitbucket.org/ecollins/passlib/] passlib是一个久经考验的非常成熟的跨平台的散列函数库,它所提供的功能包括随机盐密码的生成与验证,两步验证等。 @@ -1892,7 +1892,7 @@ Home:[http://lxml.de/]

beautifulsoup

-Home:[https://www.crummy.com/software/BeautifulSoup/bs4/doc/] +Home:[https://www.crummy.com/software/BeautifulSoup/] Links:[https://zh.wikipedia.org/zh-cn/Beautiful_Soup 维基百科] From 15a95bed36fff8df2b5da4304cc421e523ed1206 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 22:44:20 +0800 Subject: [PATCH 07/25] =?UTF-8?q?=E8=B0=83=E6=95=B4=E2=80=9D=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=B3=BB=E7=BB=9F=E2=80=9C=E4=B8=8B=E7=BA=A7=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 94 ++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index a4703d7..746b584 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -378,53 +378,7 @@ if pid == 0 : # 子进程 用于进程信号处理的标准库(主要用于 Linux & UNIX 系统)。 -== 3.5 Linux & Unix 系统相关 == - -

syslog

- -【标准库】 - -通过它可以很方便地跟 POSIX 的 syslog 服务进行交互。 - -== 3.6 Windows 系统相关 == - -

PyWin32

- -Home:[http://python.net/crew/mhammond/win32/] - -这个第三方库封装了 Windows API 及 COM API。通过它可以方便地用 Python 进行 Windows 编程(调用 COM 组件、编写 Windows 服务、等)。 - -== 3.7 程序打包 == - -

PyInstaller

- -Home:[http://www.pyinstaller.org/] - -PyInstaller 可以把你的 Python 代码制作成独立运行的程序(不依赖 Python 环境就可以运行)。 - -该工具支持多种操作系统,包括:Windows、Linux、Mac OS X、Solaris、AIX、等。 - -

py2exe

- -Home:[http://www.py2exe.org/] - -Links:[https://en.wikipedia.org/wiki/Py2exe Wikipedia] - -py2exe 的功能类似 PyInstaller,但只支持 Windows 平台。 - -

py2app

- -Home:[https://bitbucket.org/ronaldoussoren/py2app] - -它很类似于 [http://www.py2exe.org/ py2exe],差别在于 [http://www.py2exe.org/ py2exe] 支持 Windows 平台,而 [https://bitbucket.org/ronaldoussoren/py2app py2app] 支持 Mac OS X 平台。 - -

EasyInstall & Setuptools

- -Home:[https://pypi.python.org/pypi/setuptools] - -这套工具可以帮助你进行第三方库的管理(下载、编译、安装、升级、卸载) - -== 3.8 系统管理 == +== 3.5 获取系统信息 ==

sys

@@ -469,6 +423,52 @@ psutil.cpu_freq() # 获取 CPU 频率 psutil.virtual_memory() # 获取内存信息 +== 3.6 Linux & Unix 系统相关 == + +

syslog

+ +【标准库】 + +通过它可以很方便地跟 POSIX 的 syslog 服务进行交互。 + +== 3.7 Windows 系统相关 == + +

PyWin32

+ +Home:[http://python.net/crew/mhammond/win32/] + +这个第三方库封装了 Windows API 及 COM API。通过它可以方便地用 Python 进行 Windows 编程(调用 COM 组件、编写 Windows 服务、等)。 + +== 3.8 程序打包 == + +

PyInstaller

+ +Home:[http://www.pyinstaller.org/] + +PyInstaller 可以把你的 Python 代码制作成独立运行的程序(不依赖 Python 环境就可以运行)。 + +该工具支持多种操作系统,包括:Windows、Linux、Mac OS X、Solaris、AIX、等。 + +

py2exe

+ +Home:[http://www.py2exe.org/] + +Links:[https://en.wikipedia.org/wiki/Py2exe Wikipedia] + +py2exe 的功能类似 PyInstaller,但只支持 Windows 平台。 + +

py2app

+ +Home:[https://bitbucket.org/ronaldoussoren/py2app] + +它很类似于 [http://www.py2exe.org/ py2exe],差别在于 [http://www.py2exe.org/ py2exe] 支持 Windows 平台,而 [https://bitbucket.org/ronaldoussoren/py2app py2app] 支持 Mac OS X 平台。 + +

EasyInstall & Setuptools

+ +Home:[https://pypi.python.org/pypi/setuptools] + +这套工具可以帮助你进行第三方库的管理(下载、编译、安装、升级、卸载) + ---- = 4 Web = From ee28561782845c2e240408a3cdfcc8f6ac71feb8 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 22:54:31 +0800 Subject: [PATCH 08/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=20yagmail=20=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index 746b584..b1e75c0 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -870,7 +870,7 @@ Home:[https://github.com/kootenpv/yagmail] import yagmail -yag = yagmail.SMTP() +yag = yagmail.SMTP("my_gmail_username", "my_gmail_password") contents = ["This is the body, and here is just text http://somedomain/image.png", "You can find an audio file attached.', '/local/path/song.mp3"] yag.send("to@someone.com", "subject", contents) From d5c6f40db76f360f52c7c25e9287b3cb75f796ad Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 23:03:35 +0800 Subject: [PATCH 09/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BA=93=E7=9A=84=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index b1e75c0..ef1b208 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -612,7 +612,7 @@ Home:[http://flask.pocoo.org/] Links:[https://zh.wikipedia.org/wiki/Flask 维基百科] -轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2 模板引擎。 +轻量级 Web 应用框架。基于 Werkzeug WSGI 工具箱和 Jinja2 模板引擎。 代码示例——Hello world @@ -633,7 +633,7 @@ Home:[http://www.tornadoweb.org/] Links:[https://zh.wikipedia.org/wiki/Tornado 维基百科] -同样也是轻量级的Web框架,类似Web.py,但是异步非阻塞IO的处理方式使其拥有较为出色的抗负载能力。 +同样也是轻量级的 Web 框架,类似 Web.py。提供异步非阻塞 IO 的处理方式使其拥有较为出色的抗负载能力。 == 4.4 Web前端 & JS整合 == @@ -710,7 +710,7 @@ Home:[https://github.com/google/pywebsocket] Home:[http://www.seleniumhq.org/] -selenium是一个非常优秀的用于爬虫、Web自动化测试测试的框架。 +selenium 是一个非常优秀的框架,用于爬虫和 Web 自动化测试。 代码示例——模拟登录 @@ -729,7 +729,9 @@ Home:[https://scrapy.org/] Links:[https://en.wikipedia.org/wiki/Scrapy Wikipedia] -Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 +Scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 + +可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。 ---- @@ -900,7 +902,7 @@ Home:[https://github.com/eternnoir/pyTelegramBotAPI] Home:[https://github.com/LonamiWebs/Telethon] -纯 Python 3 的 [https://telegram.org/ Telegram] 客户端封装。 +纯 Python3 的 [https://telegram.org/ Telegram] 客户端封装。 === 5.3.6 远程控制 === @@ -944,7 +946,9 @@ Home:[https://github.com/citronneur/rdpy] Home:[http://www.paramiko.org/] -paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。除了SSH协议之外,paramiko还支持SFTP。 +paramiko 是用 python 语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。 + +除了 SSH 协议之外,paramiko 还支持SFTP。 代码示例 @@ -1671,7 +1675,9 @@ Home:[http://code.enthought.com/chaco/] Home:[https://plot.ly/] -plotly是现代平台的敏捷商业智能和数据科学库,它作为一款开源的绘图库,可以应用于Python、R、MATLAB、Excel、JavaScript和jupyter等多种语言 +plotly 是现代平台的敏捷商业智能和数据科学库。 + +它作为一款开源的绘图库,可以应用于 Python、R、MATLAB、Excel、JavaScript、jupyter 等多种语言。 ---- @@ -1719,7 +1725,7 @@ Home:[https://github.com/google/keyczar] Home:[https://bitbucket.org/ecollins/passlib/] -passlib是一个久经考验的非常成熟的跨平台的散列函数库,它所提供的功能包括随机盐密码的生成与验证,两步验证等。 +passlib 是一个久经考验的非常成熟的跨平台的散列函数库,它所提供的功能包括随机盐密码的生成与验证,两步验证等。 代码示例——验证随机盐密码 @@ -1896,7 +1902,9 @@ Home:[https://www.crummy.com/software/BeautifulSoup/] Links:[https://zh.wikipedia.org/zh-cn/Beautiful_Soup 维基百科] -Beautiful Soup 是一个可以从HTML 或XML 文件中提取数据的Python 库,爬虫利器,通常与requests或者selenium配合。 +Beautiful Soup 可以从 HTML 或 XML 文件中提取数据。 + +它是写”爬虫“的利器,通常与 requests 或 selenium 配合。 == 9.4 PDF == From 81943cd673508d4b04d4352f498028287dd992f0 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 27 Apr 2018 23:26:15 +0800 Subject: [PATCH 10/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=20psutil=20=E7=9A=84?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=E5=92=8C=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index ef1b208..4ecc184 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -412,7 +412,9 @@ platform.architecture() # 获取操作系统的架构,例如:("32bit", "Win Home:[https://github.com/giampaolo/psutil] -psutil(Python system and process utilities)是一个跨平台的进程管理和系统工具的python库,可以处理系统CPU,memory,disks,network等信息。主要用于系统资源的监控,分析,以及对进程进行一定的管理。 +psutil(Python system and process utilities)是一个跨平台的进程管理和系统工具库,可以处理”CPU、内存、磁盘、网络、用户“等信息。 + +主要用于系统资源的监控,分析,以及对进程进行一定的管理。 代码示例 @@ -420,7 +422,20 @@ import psutil psutil.cpu_count() # 获取 CPU 数量 psutil.cpu_freq() # 获取 CPU 频率 + psutil.virtual_memory() # 获取内存信息 +psutil.swap_memory() # 获取交换分区(换页文件)信息 + +psutil.disk_partitions() # 获取分区信息 +psutil.disk_usage('/') # 获取某分区的使用情况 + +psutil.users() # 获取用户信息 + +p = psutil.Process(pid) # 根据给定的 pid 获得进程对象 +p.name() # 进程名 +p.exe() # 可执行程序的全路径 +p.cwd() # 进程的当前目录 +p.cmdline() # 启动进程的命令行参数 == 3.6 Linux & Unix 系统相关 == From 53ae70d5fd0b3d15a2a3384660d77e766b2f0371 Mon Sep 17 00:00:00 2001 From: programthink Date: Sat, 4 Aug 2018 19:56:14 +0800 Subject: [PATCH 11/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20cpr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 8ad9411..3054f20 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -2517,6 +2517,28 @@ Docs:[http://pocoproject.org/docs/package-Net.HTTPClient.html] POCO 前面已经介绍过。它提供了 HTTP Client 的封装类。 +

cpr

+ +Home:[https://github.com/whoshuu/cpr] + +类似 Python 的 [http://docs.python-requests.org/ Requests 库],以【优雅的语法】实现 HTTP 请求。 + +代码示例——HTTP GET + +#include + +int main() +{ + auto r = cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"}, + cpr::Authentication{"user", "pass"}, + cpr::Parameters{{"anon", "true"}, {"key", "value"}}); + r.status_code; // 200 + r.header["content-type"]; // application/json; charset=utf-8 + r.text; // JSON text string + return 0; +} + + == 10.3 浏览器引擎 ==

WebKit

From e41f440b31e25e32faee7250a107b47db8942802 Mon Sep 17 00:00:00 2001 From: programthink Date: Sat, 4 Aug 2018 20:23:20 +0800 Subject: [PATCH 12/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20docopt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 3054f20..2bef0f4 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -1388,6 +1388,57 @@ Boost 前面已经介绍过。这是 Boost 的其中一个子库,提供了“ 它的功能很丰富,但是比较重型。 +

docopt

+ +Home:[https://github.com/docopt/docopt.cpp] + +这是一个很有创意的库,提供了一种【全新】的方式来处理命令行参数。 + +你只需定义好命令行的帮助文本(usage),该库会根据 usage 文本自动分析出命令行参数的定义。 + +也就是说:用了这个库,就无需再写一大堆 parse 命令行参数的代码 :) + +代码示例 + +#include +#include + +static const char USAGE[] = +R"(Naval Fate. + + Usage: + naval_fate ship new ... + naval_fate ship move [--speed=] + naval_fate ship shoot + naval_fate mine (set|remove) [--moored | --drifting] + naval_fate (-h | --help) + naval_fate --version + + Options: + -h --help Show this screen. + --version Show version. + --speed= Speed in knots [default: 10]. + --moored Moored (anchored) mine. + --drifting Drifting mine. +)"; + +int main(int argc, const char* argv[]) +{ + std::map args + = docopt::docopt(USAGE, + { argv + 1, argv + argc }, + true, // 是否显示帮助 + "Naval Fate 2.0"); // 版本号 + + for(auto const& arg : args) + { + std::cout << arg.first << arg.second << std::endl; + } + +return 0; +} + + == 7.2 文本终端 ==

ncurses

From 1aab23863384ead7e4c2a8ed44d628bc1b0bb1e0 Mon Sep 17 00:00:00 2001 From: programthink Date: Sat, 4 Aug 2018 20:57:24 +0800 Subject: [PATCH 13/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20simple=5Fmatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 2bef0f4..fb32af5 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -753,6 +753,35 @@ vector v; for_each(v.begin(), v.end(), cout << boost::lambda::_1 << "\n"); +

simple_match

+ +Home:[https://github.com/jbandela/simple_match] + +这个库是为了提供类似 Rust 语言中的 match 语法。 + +(不懂 Rust 的同学,可以把 match 语法视作增强版的 switch/case) + +代码示例 + +#include +#include +using namespace simple_match; +using namespace simple_match::placeholders; + +int x = 0; +while (true) +{ + std::cin >> x; + match(x, + 1, []() { std::cout << "one\n"; }, + 2, []() { std::cout << "two\n"; }, + _x < 10, [](auto&& n) { std::cout << n << " is less than 10\n"; }, + 10 < _x < 20, [](auto&& n) { std::cout << n << " is between 10 and 20 exclusive\n"; }, + _, []() { std::cout << "NOT match\n"; } + ); +} + + == 3.4 元编程(Metaprogramming) == (不知道何为“元编程”,可以先看[https://zh.wikipedia.org/wiki/%E5%85%83%E7%BC%96%E7%A8%8B 维基百科]) From 490156aaa53e7769694b5cd4cede737ef8065ece Mon Sep 17 00:00:00 2001 From: programthink Date: Sat, 4 Aug 2018 21:08:10 +0800 Subject: [PATCH 14/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Poppler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index fb32af5..0103134 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -3073,6 +3073,18 @@ Home:[http://podofo.sourceforge.net/] 它既支持 PDF 文件的生成,也支持 PDF 内容的提取。它同时还提供[http://podofo.sourceforge.net/tools.html 一堆命令行的小工具],用来操作 PDF 文件。 +

Poppler

+ +Home:[https://poppler.freedesktop.org/] + +Links:[https://en.wikipedia.org/wiki/Poppler_%28software%29 Wikipedia] + +派生自 Xpdf 3.0 的开源库,用于渲染 PDF 的内容。很多开源的 PDF 阅读软件用到它。 + +在开源界,它第一个【完整】实现了 ISO 32000-1(PDF 的 ISO 标准)。 + +它内置了若干命令行工具(poppler-utils),可以用来进行 PDF 的内容提取和格式转换。 +

LibHaru

Home:[http://libharu.org/] From 9b71c58132fab40f4de0719e9f17c7b913466e2b Mon Sep 17 00:00:00 2001 From: programthink Date: Sat, 4 Aug 2018 21:57:56 +0800 Subject: [PATCH 15/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Scintilla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 0103134..e4a6bd2 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -1330,6 +1330,18 @@ Home:[http://wxmathplot.sourceforge.net/] 看名称就知道它是跟 wxWidgets 搭配的。效果图参见“[http://wxmathplot.sourceforge.net/screenshot.shtml 这里]” +== 6.3 文本编辑 == + +

Scintilla

+ +Home:[https://scintilla.org/] + +Links:[https://en.wikipedia.org/wiki/Scintilla_(software) Wikipedia] + +功能【超强】的文本编辑组件,支持:词法高亮、代码折叠、自动补全、动态提示......(基本上你能想到的,它都已经有了) + +不光功能齐全,还支持各种主流的操作系统平台。很多编辑器/IDE 用到它。 + ---- = 7 文本用户界面(TUI) = From 143c3755f1d398e04147a33666d661e19a9d610c Mon Sep 17 00:00:00 2001 From: programthink Date: Sun, 5 Aug 2018 17:31:25 +0800 Subject: [PATCH 16/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Cython?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index e4a6bd2..58a62a1 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -3650,6 +3650,16 @@ BOOST_PYTHON_MODULE(hello_ext) // print(hello_ext.greet()) +

Cython

+ +Home:[http://cython.org/] + +Links:[https://en.wikipedia.org/wiki/Cython Wikipedia] + +Cython 提供了一种机制(编译器)编译 Python 代码为【原生代码】。编译后的原生代码是个 Python 的 module(扩展名是 pyd 或 so),可以在常规的 Python 环境中用 import 语句加载。如此一来,既得到了类似 C/C++ 的高性能,又保留了 Python 语法的简介性。 + +另外,它也提供了把 C/C++ 代码编译为 Python 模块的机制。 + ---- = 18 (其它) = From 03120d5d4ec8c6b4196809b95cedfedff28be5fc Mon Sep 17 00:00:00 2001 From: programthink Date: Sun, 5 Aug 2018 18:05:07 +0800 Subject: [PATCH 17/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20V8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 58a62a1..3efc90e 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -3660,6 +3660,72 @@ Cython 提供了一种机制(编译器)编译 Python 代码为【原生代 另外,它也提供了把 C/C++ 代码编译为 Python 模块的机制。 +=== 17.2.2 整合 JavaScript 语言 === + +

V8

+ +Home:[https://github.com/v8/v8] + +Links:[https://en.wikipedia.org/wiki/Chrome_V8 Wikipedia] + +这就是大名鼎鼎的 V8 引擎,Google 公司开发,C++ 编写,被用于 Chrome 浏览器。 + +既然用于 Chrome 浏览器,显然【完整地】支持了 JS 语言的规范(ECMA-262)。 + +它既可以独立运行,也可以嵌入到 C++ 程序中。 + +代码示例——Hello world + +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // Initialize V8. + v8::V8::InitializeICUDefaultLocation(argv[0]); + v8::V8::InitializeExternalStartupData(argv[0]); + std::unique_ptr platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + v8::V8::Initialize(); + + // Create a new Isolate and make it the current one. + v8::Isolate::CreateParams create_params; + create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + v8::Isolate* isolate = v8::Isolate::New(create_params); + + { + v8::Isolate::Scope isolate_scope(isolate); + // Create a stack-allocated handle scope. + v8::HandleScope handle_scope(isolate); + // Create a new context. + v8::Local context = v8::Context::New(isolate); + // Enter the context for compiling and running the hello world script. + v8::Context::Scope context_scope(context); + // Create a string containing the JavaScript source code. + v8::Local source = + v8::String::NewFromUtf8(isolate, "'Hello World!'", + v8::NewStringType::kNormal).ToLocalChecked(); + // Compile the source code. + v8::Local script = v8::Script::Compile(context, source).ToLocalChecked(); + // Run the script to get the result. + v8::Local result = script->Run(context).ToLocalChecked(); + // Convert the result to an UTF8 string and print it. + v8::String::Utf8Value utf8(isolate, result); + printf("%s\n", *utf8); + } + + // Dispose the isolate and tear down V8. + isolate->Dispose(); + v8::V8::Dispose(); + v8::V8::ShutdownPlatform(); + delete create_params.array_buffer_allocator; + return 0; +} + + ---- = 18 (其它) = From 5810f1337566397ec488ccf57888e6359634ee1c Mon Sep 17 00:00:00 2001 From: programthink Date: Sun, 5 Aug 2018 18:51:55 +0800 Subject: [PATCH 18/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20nbind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/cpp.wiki | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/libs/cpp.wiki b/libs/cpp.wiki index 3efc90e..723d5c9 100644 --- a/libs/cpp.wiki +++ b/libs/cpp.wiki @@ -3726,6 +3726,57 @@ int main(int argc, char* argv[]) } +

nbind

+ +Home:[https://github.com/charto/nbind] + +把 C++ 代码编译为 JS 的 package。编译方式支持“原生二进制”或“asm.js”。 + +编译需要依赖 Node.js,运行环境支持 Node.js 或浏览器。 + +代码示例——C++ 代码 + +#include +#include + +struct Greeter +{ + static void sayHello(std::string name) + { + std::cout << "Hello, " << name << "\n"; + } +}; + +// 建立绑定 +#include + +NBIND_CLASS(Greeter) +{ + method(sayHello); +} + + +代码示例——node.js 环境调用代码 + +var nbind = require("nbind"); +var lib = nbind.init().lib; +lib.Greeter.sayHello("you"); + + +代码示例——浏览器环境调用代码 + + + + + ---- = 18 (其它) = From 315f1f0af8653695468a17c626081de7fbf20764 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 10 Aug 2018 16:53:04 +0800 Subject: [PATCH 19/25] =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E8=A1=8C=E9=A6=96=20#=20=E5=AD=97=E7=AC=A6=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=20HTML=20=E8=BD=AC=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index 4ecc184..73581bc 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -769,7 +769,7 @@ Links:[https://en.wikipedia.org/wiki/Scapy Wikipedia] # 传统的 ping 扫描(网络层) ans,unans = sr(IP(dst="192.168.1.1-254")/ICMP()) -# 局域网内的 ARP 扫描(链路层) +# 局域网内的 ARP 扫描(链路层) ans,unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst="192.168.1.0/24"), timeout=2) @@ -1796,14 +1796,14 @@ JSON 格式源自 JavaScript,如今在 Web 开发中广为应用。 import json json.dumps(["foo", {"bar": ("baz", None, 1.0, 2)}]) -# JSON 编码 -# 得到如下【字符串】 -# """["foo", {"bar": ["baz", null, 1.0, 2]}]""" +# JSON 编码 +# 得到如下【字符串】 +# """["foo", {"bar": ["baz", null, 1.0, 2]}]""" json.loads("""["foo", {"bar":["baz", null, 1.0, 2]}]""") -# JSON 解码 -# 得到如下【对象】 -# [u"foo", {u"bar": [u"baz", None, 1.0, 2]}] +# JSON 解码 +# 得到如下【对象】 +# [u"foo", {u"bar": [u"baz", None, 1.0, 2]}] === 9.1.3 YAML === @@ -2119,11 +2119,11 @@ Home:[https://github.com/xflr6/graphviz] from graphviz import Digraph dot = Digraph(comment='The Round Table') -# 添加节点 +# 添加节点 dot.node('A', 'King Arthur') dot.node('B', 'Sir Bedevere the Wise') dot.node('L', 'Sir Lancelot the Brave') -# 添加连线 +# 添加连线 dot.edges(['AB', 'AL']) dot.edge('B', 'L', constraint='false') @@ -2217,7 +2217,7 @@ c = [] for i in range(len(a)) : c.append(a[i] + b[i]) -# 以下是 NumPy 的写法,简洁且速度飞快 +# 以下是 NumPy 的写法,简洁且速度飞快 import numpy as np a = np.arange(10000000) b = np.arange(10000000) From 446858bfbe61290e7d37ccdd6f982ddf401add15 Mon Sep 17 00:00:00 2001 From: programthink Date: Fri, 10 Aug 2018 17:16:31 +0800 Subject: [PATCH 20/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20keyboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index 73581bc..fa715d4 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -378,7 +378,35 @@ if pid == 0 : # 子进程 用于进程信号处理的标准库(主要用于 Linux & UNIX 系统)。 -== 3.5 获取系统信息 == +== 3.5 操作硬件 == + +

keyboard

+ +Home:[https://github.com/boppreh/keyboard] + +顾名思义,这个库让你可以进行各种键盘相关的操作,包括:模拟按键、键盘钩子(hook),按键记录及重放。 + +支持复杂的组合键。纯 python 代码代码,同时支持 Windows 和 Linux。 + +代码示例 + +import keyboard + +# 模拟按键。 +keyboard.press_and_release("shift+s, space") + +# 模拟按键,并执行相应代码。 +keyboard.add_hotkey("page up, page down", lambda: keyboard.write("xxxx")) + +# 等待特定按键,然后继续执行。 +keyboard.wait("esc") + +# 记录按键,直到用户按了 ESC;然后以3倍速重放刚才记录的按键。 +recorded = keyboard.record(until="esc") +keyboard.play(recorded, speed_factor=3) + + +== 3.6 获取系统信息 ==

sys

@@ -438,7 +466,7 @@ p.cwd() # 进程的当前目录 p.cmdline() # 启动进程的命令行参数 -== 3.6 Linux & Unix 系统相关 == +== 3.7 Linux & Unix 系统相关 ==

syslog

@@ -446,7 +474,7 @@ p.cmdline() # 启动进程的命令行参数 通过它可以很方便地跟 POSIX 的 syslog 服务进行交互。 -== 3.7 Windows 系统相关 == +== 3.8 Windows 系统相关 ==

PyWin32

@@ -454,7 +482,7 @@ Home:[http://python.net/crew/mhammond/win32/] 这个第三方库封装了 Windows API 及 COM API。通过它可以方便地用 Python 进行 Windows 编程(调用 COM 组件、编写 Windows 服务、等)。 -== 3.8 程序打包 == +== 3.9 程序打包 ==

PyInstaller

From 34bb7d079596f44c70d3b1eef017ba45e76d5c0c Mon Sep 17 00:00:00 2001 From: programthink Date: Tue, 30 Oct 2018 23:10:19 +0800 Subject: [PATCH 21/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9DWebSocket?= =?UTF-8?q?=E2=80=9C=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/python.wiki b/libs/python.wiki index fa715d4..86290b2 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -739,16 +739,18 @@ Home:[https://github.com/jmalonzo/pywebkitgtk] PyWebKitGtk 则提供了对 WebKitGtk 的 Python 封装。 -== 4.6 (其它) == +== 4.6 WebSocket ==

pywebsocket

Home:[https://github.com/google/pywebsocket] -这是 Google 提供的 [https://zh.wikipedia.org/wiki/WebSocket WebSocket] 服务端。 +这是 Google 提供的 [https://zh.wikipedia.org/wiki/WebSocket WebSocket]【服务端】。 该项目包含一个可独立运行的 server 以及一个 Apache 扩展模块(mod_pywebsocket)。 +== 4.7 (其它) == +

selenium

Home:[http://www.seleniumhq.org/] From 73c27911a3c243ebd03a31d33b2aecf61113c43f Mon Sep 17 00:00:00 2001 From: programthink Date: Tue, 30 Oct 2018 23:27:19 +0800 Subject: [PATCH 22/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ws4py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index 86290b2..d83820e 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -741,14 +741,23 @@ PyWebKitGtk 则提供了对 WebKitGtk 的 Python 封装。 == 4.6 WebSocket == +(关于 WebSocket 的介绍,可以参见维基百科的“[https://zh.wikipedia.org/wiki/WebSocket 这个链接]”)

pywebsocket

Home:[https://github.com/google/pywebsocket] -这是 Google 提供的 [https://zh.wikipedia.org/wiki/WebSocket WebSocket]【服务端】。 +这是 Google 提供的 WebSocket【服务端】。 该项目包含一个可独立运行的 server 以及一个 Apache 扩展模块(mod_pywebsocket)。 +

ws4py

+ +Home:[https://github.com/Lawouach/WebSocket-for-Python] + +此项目同时提供了 WebSocket 的“服务端 & 客户端”;并同时支持 Python2 和 Python3。 + +其【服务端】可以跟“CherryPy、gevent、wsgiref、asyncio”整合;其【客户端】可以跟“Tornado、gevent”整合。 + == 4.7 (其它) ==

selenium

From bf30db21fb845086432e9d989ea5cc818f9b4ede Mon Sep 17 00:00:00 2001 From: programthink Date: Tue, 30 Oct 2018 23:46:23 +0800 Subject: [PATCH 23/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20AutobahnPython?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index d83820e..e573f04 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -750,11 +750,45 @@ Home:[https://github.com/google/pywebsocket] 该项目包含一个可独立运行的 server 以及一个 Apache 扩展模块(mod_pywebsocket)。 +

AutobahnPython

+ +Home:[https://github.com/crossbario/autobahn-python] + +这是 [http://crossbar.io/autobahn Autobahn] 项目的子项目,同时提供了 WebSocket 的“服务端 & 客户端”。 + +它兼容 Python2 和 Python3,另外还兼容 PyPy 和 Jython。 + +网络方面,它可以跟“asyncio 标准库”以及“[https://en.wikipedia.org/wiki/Twisted_%28software%29 Twisted]”整合。 + +除了实现 WebSocket 协议,它还完整实现了 WAMP(Web Application Messaging Protocol)。 + +代码示例——Echo Server + +from autobahn.twisted.websocket import WebSocketServerProtocol + +class MyServerProtocol(WebSocketServerProtocol) : + def onConnect(self, request) : + print("Client connecting: {}".format(request.peer)) + + def onOpen(self) : + print("WebSocket connection open.") + + def onMessage(self, payload, isBinary) : + if isBinary : + print("Binary message received: {} bytes".format(len(payload))) + else: + print("Text message received: {}".format(payload.decode("utf8"))) + self.sendMessage(payload, isBinary) + + def onClose(self, wasClean, code, reason) : + print("WebSocket connection closed: {}".format(reason)) + +

ws4py

Home:[https://github.com/Lawouach/WebSocket-for-Python] -此项目同时提供了 WebSocket 的“服务端 & 客户端”;并同时支持 Python2 和 Python3。 +此项目同时提供了 WebSocket 的“服务端 & 客户端”;并同时兼容 Python2 和 Python3。 其【服务端】可以跟“CherryPy、gevent、wsgiref、asyncio”整合;其【客户端】可以跟“Tornado、gevent”整合。 From c9ef0a0dfc06716b7ac43d71e0d7cf33c0212a4d Mon Sep 17 00:00:00 2001 From: programthink Date: Tue, 30 Oct 2018 23:59:30 +0800 Subject: [PATCH 24/25] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/python.wiki b/libs/python.wiki index e573f04..6d00d34 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -349,6 +349,18 @@ if __name__ == "__main__" : Process(target=f, args=(lock, num)).start() +

sh

+ +Home:[https://github.com/amoffat/sh] + +这个项目可以用来取代标准库中的 subprocess;同时兼容 Python2 和 Python3。 + +代码示例 + +from sh import ifconfig +print(ifconfig("wlan0")) + + == 3.4 本地进程间通信(IPC) ==

mmap

From 23adccf2dc6f2d4ca74abc136e1a94777c456276 Mon Sep 17 00:00:00 2001 From: programthink Date: Wed, 31 Oct 2018 00:13:00 +0800 Subject: [PATCH 25/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=20sh=20=E7=9A=84?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=E5=92=8C=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/python.wiki | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libs/python.wiki b/libs/python.wiki index 6d00d34..5dd27e0 100644 --- a/libs/python.wiki +++ b/libs/python.wiki @@ -355,12 +355,29 @@ Home:[https://github.com/amoffat/sh] 这个项目可以用来取代标准库中的 subprocess;同时兼容 Python2 和 Python3。 -代码示例 +使用它可以写出比 subprocess 更简洁、更优雅的代码。 + +代码示例——获取命令输出 from sh import ifconfig print(ifconfig("wlan0")) +代码示例——命令行参数 + +from sh import curl +# 传统风格 +curl("https://program-think.blogspot.com/", "-o", "test.html", "--silent") +# 命名参数风格 +curl("https://program-think.blogspot.com/", o="test.html", silent=True) + + +代码示例——管道 + +from sh import ls, wc +print(wc(ls("/etc", "-1"), "-l")) + + == 3.4 本地进程间通信(IPC) ==

mmap