设为首页 收藏本站
查看: 622|回复: 0

[经验分享] Python Lover(6)Twisted and Basic

[复制链接]

尚未签到

发表于 2017-4-29 08:19:50 | 显示全部楼层 |阅读模式
Python Lover(6)Twisted and Basic

1. Installation and Version Upgrade on Python
Download the package from official website. I get these 2 version, 2.7.10 and 3.4.3 for mac.
https://www.python.org/

Verify the Installation
> python -V
Python 2.7.6
> python2 -V
Python 2.7.10
> python3 -V
Python 3.4.3

tools pip http://zhonghuan.info/2014/10/01/pip%E4%BB%8B%E7%BB%8D%E4%B8%8E%E4%BD%BF%E7%94%A8/
Set up the right version, I plan to use 2.7.10, because a lot of tools and other things, they are not supporting python3 right now.
> rm -fr /usr/local/bin/python
> rm -fr /usr/bin/python
> sudo ln -s /usr/local/bin/python2 /usr/bin/python

> python -V
Python 2.7.10

Download pip file
> wget https://bootstrap.pypa.io/get-pip.py

Install pip
> python get-pip.py

Verify the version
> pip -V
pip 7.1.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)

Install or upgrade setuptools
> pip install -U setuptools

Upgrade pip if needed
> pip install -U pip

Use pip to install latest Django
> pip install Django==1.8.4

Verify the Django installation
> python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.8.4

How we use Django there
http://sillycat.iteye.com/blog/2116834
http://sillycat.iteye.com/blog/2116836

Download and use pycharm for Python IDE
https://www.jetbrains.com/pycharm/

Download Twisted
> wget http://twistedmatrix.com/Releases/Twisted/15.4/Twisted-15.4.0.tar.bz2

Unzip the file and go to the working directory
> cd Twisted-15.4.0

Install from the source
> python setup.py install

Check the version after installation.
>>> print twisted.version
[twisted, version 15.4.0]

2. Understand Model
single-threaded synchronous     task1 —> task2 —> task3
the threaded model                    task1 —>
                                                   task2 —>
                                                   task3 —>
Asynchronous model                 task1 —> task2 —>task1 —>task3 —>task2 —>task1 ...

The last model works best>
a. There are a large number of tasks so there is likely always at least one task that can make progress.
b. The tasks perform lots of I/O, causing a synchronous program to waste lots of time blocking
c. The tasks are largely independent

working on these links
http://twistedmatrix.com/documents/current/web/howto/
http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html

http://lingxiankong.github.io/blog/2013/12/23/python-setup/
http://developer.iyunv.com/art/201003/189317.htm
http://yansu.org/2013/05/15/learn-how-to-use-distutils.html
http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/

browser ——> request/response ——>HTTP   render, Template system.

3. Objects and Twisted
Site Objects
from twisted.web import server, resource
from twisted.internet import reactor

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>Hello, Sillycat.</html>"

site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()

Visit this URL will work http://localhost:8080/

Twistd web Server
> twistd web --help
Usage: twistd [options] web [web options]

Here is the command to start twistd server
> twistd web --path ./ --port 8080 --logfile ./twistd.log

Then, we can visit http://localhost:8080/ to see the current directory.

This command will kill the process
> kill `cat twistd.pid`

http://twistedmatrix.com/documents/current/index.html

http://twistedmatrix.com/documents/current/web/howto/web-in-60/

Dynamic Web Content
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time

class ClockPage(Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html><body>%s</body></html>" % (time.ctime(),)

resource = ClockPage()
factory = Site(resource)
reactor.listenTCP(8880, factory)
reactor.run()

URL Dispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/static-dispatch.html

from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web.static import File

root = Resource()
root.putChild("foo", File("/tmp"))
root.putChild("bar", File("/lost+found"))
root.putChild("baz", File("/opt"))

factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()

File —> Resource —>Site —> reactor
http://codereview.stackexchange.com/questions/45060/simple-rest-api-server

http://jacek99.github.io/corepost/doc/build/html/index.html

Dynamic URL Dispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/dynamic-dispatch.html

from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor

from calendar import calendar

class YearPage(Resource):
    def __init__(self, year):
        Resource.__init__(self)
        self.year = year

    def render_GET(self, request):
        return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year),)

class Calendar(Resource):
  def getChild(self, name, request):
      return YearPage(int(name))

root = Calendar()
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()

Site - the object which associates a listening server port with the HTTP implementation
Resource - a convenient base class to use when defining custom pages
reactor - the object which implements the Twisted main loop

References:
Twisted
http://krondo.com/?p=1209
https://twistedmatrix.com/trac/
https://github.com/twisted/twisted

Twisted Intro
https://github.com/jdavisp3/twisted-intro
http://krondo.com/?page_id=1327

Chinese Version
https://github.com/luocheng/twisted-intro-cn
http://turtlerbender007.appspot.com/twisted/index.html

Python Basic
http://sillycat.iteye.com/blog/562783   Basic Syntax
http://sillycat.iteye.com/blog/568997   Operator and Function
http://sillycat.iteye.com/blog/569004   If, else, while, and, or, not
http://sillycat.iteye.com/blog/569827   for, array [], string, split, join
http://sillycat.iteye.com/blog/569837   List (), random, dict {},
http://sillycat.iteye.com/blog/570849   try: except: , raise OutOfRangeError(‘out of range’)
http://sillycat.iteye.com/blog/570862   class, copy, def function in class
http://sillycat.iteye.com/blog/570882   __init__, __str__, __add__, __sub__, private function __methodName

example
https://github.com/orangain/pika-twisted-example

https://www.digitalocean.com/community/tutorials/how-to-package-and-distribute-python-applications
distutils
http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/
http://yansu.org/2013/05/15/learn-how-to-use-distutils.html
http://developer.iyunv.com/art/201003/189317.htm
http://lingxiankong.github.io/blog/2013/12/23/python-setup/

web
http://twistedmatrix.com/documents/current/web/howto/
http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370585-1-1.html 上篇帖子: 定时执行Python脚本或模型 下篇帖子: python 多线程编程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表