您的位置:首页 > 理论基础 > 计算机网络

python模块:网络协议和支持

2016-11-30 16:55 746 查看
python模块:网络协议和支持

webbrowser

调用浏览器显示html文件

webbrowser.open('map.html')


[
webbrowser
— Convenient Web-browser controller
]


皮皮blog

uuid/hmac/hashlib生成唯一ID

在有些情况下你需要生成一个唯一的字符串。我看到很多人使用md5()函数来达到此目的,但它确实不是以此为目的。

uuid是基于Python实现的UUID库,它实现了UUID标注的1,3,4和5版本,在确保唯一性上真的非常方便。 其实有一个名为uuid()的Python函数是用于这个目的的。

import uuid
result = uuid.uuid1()
print result
# output => various attempts
# 9e177ec0-65b6-11e3-b2d0-e4d53dfcf61b
# be57b880-65b6-11e3-a04d-e4d53dfcf61b
# c3b2b90f-65b6-11e3-8c86-e4d53dfcf61b

你可能会注意到,即使字符串是唯一的,但它们后边的几个字符看起来很相似。这是因为生成的字符串与电脑的MAC地址是相联系的。
为了减少重复的情况,你可以使用这两个函数。
import hmac,hashlib
key='1'
data='a'
print hmac.new(key, data, hashlib.sha256).hexdigest()
m = hashlib.sha1()
m.update("The quick brown fox jumps over the lazy dog")
print m.hexdigest()
# c6e693d0b35805080632bc2469e1154a8d1072a86557778c27a01329630f8917
# 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12

[
uuid
— UUID objects according to RFC 4122
]

smtplib 邮件模块

[
smtplib
— SMTP protocol client
]

[Python_使用smtplib和email模块发送邮件]

[python模块学习 ---- smtplib 邮件发送]

皮皮blog

其它网络相关模块

wsgiref

[Python wsgiref 模块源码浅析]

webpy

[webpy源码分析概览图]

Whitenoise

只需简单地修改Config文件,用户就可以按自己的意图来以静态文件方式部署Web应用,而不必依赖于Nginx、亚马逊S3等外部服务。Whitenoise能对打包内容进行压缩并设置高容量的缓存。

遵循WSGI规范的应用需要在部署时对Whitenoise配置进行调整:

from whitenoise import WhiteNoise

from my_project import MyWSGIApp

application = MyWSGIApp()
application = WhiteNoise(application, root='/path/to/static/files')
application.add_files('/path/to/more/static/files', prefix='more-files/')
这样做的重要性是什么?使用Gzip可有效地减少静态文件体积和页面载入。但是搜索引擎会侦测到Gzip压缩,这会导致网站不执行Gzip。所以需要透过上述修改来避免这种情况。
[Whitenoise]

Spyne

一个用于构建RPC服务的工具集,支持SOAP,JSON,XML等多种流行的协议。

现在有诸如 flask-restful 以及 django-rest-framework 等框架用于 REST 服务的开发,人们对于 REST 之外的框架似乎兴趣不大。Spyne 很好地填补了这一空白,它支持多种协议,而且本身也封装地相当好:

class HelloWorldService(ServiceBase):
@srpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(name, times):
for i in range(times):
yield 'Hello, %s' % name

application = Application([HelloWorldService],
tns='spyne.examples.hello',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11()
)
短短几行代码便实现了一个支持SOAP 1.1 协议的服务器端application,接入任何一个WSGI兼容的服务器后端就可以运行了。

[https://github.com/arskom/spyne]

benoitc/gunicorn

gunicorn ‘Green Unicorn’ is a WSGI HTTP Server for UNIX, fast clients and sleepy applications

一个Python WSGI UNIX的HTTP服务器,从Ruby的独角兽(Unicorn)项目移植。Gunicorn大致与各种Web框架兼容.

一个例子,运行你的flask app:

gunicorn myproject:app
使用起来超级简单!

[gunicorn]

retry.it,一个简单的重试库

[retry.it,一个简单的重试库]

from: [python模块:网络协议和支持 ]

ref: [Internet Protocols and Support]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: