twistedcruller 2015-09-15
PythonLover(6)TwistedandBasic
1.InstallationandVersionUpgradeonPython
Downloadthepackagefromofficialwebsite.Igetthese2version,2.7.10and3.4.3formac.
https://www.python.org/
VerifytheInstallation
>python-V
Python2.7.6
>python2-V
Python2.7.10
>python3-V
Python3.4.3
toolspiphttp://zhonghuan.info/2014/10/01/pip%E4%BB%8B%E7%BB%8D%E4%B8%8E%E4%BD%BF%E7%94%A8/
Setuptherightversion,Iplantouse2.7.10,becausealotoftoolsandotherthings,theyarenotsupportingpython3rightnow.
>rm-fr/usr/local/bin/python
>rm-fr/usr/bin/python
>sudoln-s/usr/local/bin/python2/usr/bin/python
>python-V
Python2.7.10
Downloadpipfile
>wgethttps://bootstrap.pypa.io/get-pip.py
Installpip
>pythonget-pip.py
Verifytheversion
>pip-V
pip7.1.1from/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages(python2.7)
Installorupgradesetuptools
>pipinstall-Usetuptools
Upgradepipifneeded
>pipinstall-Upip
UsepiptoinstalllatestDjango
>pipinstallDjango==1.8.4
VerifytheDjangoinstallation
>python
Python2.7.10(v2.7.10:15c95b7d81dc,May232015,09:33:12)
[GCC4.2.1(AppleInc.build5666)(dot3)]ondarwin
Type"help","copyright","credits"or"license"formoreinformation.
>>>importdjango
>>>print(django.get_version())
1.8.4
HowweuseDjangothere
DownloadandusepycharmforPythonIDE
https://www.jetbrains.com/pycharm/
DownloadTwisted
>wgethttp://twistedmatrix.com/Releases/Twisted/15.4/Twisted-15.4.0.tar.bz2
Unzipthefileandgototheworkingdirectory
>cdTwisted-15.4.0
Installfromthesource
>pythonsetup.pyinstall
Checktheversionafterinstallation.
>>>printtwisted.version
[twisted,version15.4.0]
2.UnderstandModel
single-threadedsynchronoustask1—>task2—>task3
thethreadedmodeltask1—>
task2—>
task3—>
Asynchronousmodeltask1—>task2—>task1—>task3—>task2—>task1...
Thelastmodelworksbest>
a.Therearealargenumberoftaskssothereislikelyalwaysatleastonetaskthatcanmakeprogress.
b.ThetasksperformlotsofI/O,causingasynchronousprogramtowastelotsoftimeblocking
c.Thetasksarelargelyindependent
workingontheselinks
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.51cto.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——>HTTPrender,Templatesystem.
3.ObjectsandTwisted
SiteObjects
fromtwisted.webimportserver,resource
fromtwisted.internetimportreactor
classSimple(resource.Resource):
isLeaf=True
defrender_GET(self,request):
return"<html>Hello,Sillycat.</html>"
site=server.Site(Simple())
reactor.listenTCP(8080,site)
reactor.run()
VisitthisURLwillworkhttp://localhost:8080/
TwistdwebServer
>twistdweb--help
Usage:twistd[options]web[weboptions]
Hereisthecommandtostarttwistdserver
>twistdweb--path./--port8080--logfile./twistd.log
Then,wecanvisithttp://localhost:8080/toseethecurrentdirectory.
Thiscommandwillkilltheprocess
>kill`cattwistd.pid`
http://twistedmatrix.com/documents/current/index.html
http://twistedmatrix.com/documents/current/web/howto/web-in-60/
DynamicWebContent
fromtwisted.internetimportreactor
fromtwisted.web.serverimportSite
fromtwisted.web.resourceimportResource
importtime
classClockPage(Resource):
isLeaf=True
defrender_GET(self,request):
return"<html><body>%s</body></html>"%(time.ctime(),)
resource=ClockPage()
factory=Site(resource)
reactor.listenTCP(8880,factory)
reactor.run()
URLDispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/static-dispatch.html
fromtwisted.web.serverimportSite
fromtwisted.web.resourceimportResource
fromtwisted.internetimportreactor
fromtwisted.web.staticimportFile
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
DynamicURLDispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/dynamic-dispatch.html
fromtwisted.web.serverimportSite
fromtwisted.web.resourceimportResource
fromtwisted.internetimportreactor
fromcalendarimportcalendar
classYearPage(Resource):
def__init__(self,year):
Resource.__init__(self)
self.year=year
defrender_GET(self,request):
return"<html><body><pre>%s</pre></body></html>"%(calendar(self.year),)
classCalendar(Resource):
defgetChild(self,name,request):
returnYearPage(int(name))
root=Calendar()
factory=Site(root)
reactor.listenTCP(8880,factory)
reactor.run()
Site-theobjectwhichassociatesalisteningserverportwiththeHTTPimplementation
Resource-aconvenientbaseclasstousewhendefiningcustompages
reactor-theobjectwhichimplementstheTwistedmainloop
References:
Twisted
http://krondo.com/?p=1209
https://twistedmatrix.com/trac/
https://github.com/twisted/twisted
TwistedIntro
https://github.com/jdavisp3/twisted-intro
http://krondo.com/?page_id=1327
ChineseVersion
https://github.com/luocheng/twisted-intro-cn
http://turtlerbender007.appspot.com/twisted/index.html
PythonBasic
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.51cto.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