Firefly笔记一之http模块
firefly筆記一之http模塊
Firefly是免費開源的游戲服務器端框架,開發語言是python,基于twisted框架開發,作為一名前端人員都有一顆后端的夢。。希望能堅持下去一直把自己的學習新東西的過程記錄下來,不對的地方能夠及時一起探討。
首先確認好Firefly的搭建環境已經OK,沒有搭建的可以參考官方網站:
http://www.9miao.com/forum.php?mod=viewthread&tid=33009&fromuid=87
http://www.9miao.com/forum.php?mod=viewthread&tid=43956&fromuid=87
? ? ?為了更好的熟悉整個流程以及學習(這正是開源的力量)Firefly我們先從頭新建一個工程一步步來
1:創建工程?
?
2:由于新建的工程默認是支持一個長連接的配置,所以先把配置改成web的配置
(1)修改config.json文件 把testserver的key改為我們的webport:
?
?
?
?
(2)修改app/apptest.py
(3) 啟動Firefly?
? ? ? (4)(打開瀏覽器輸入http://localhost:1000/snoy)(因為我是遠程其他電腦所以IP是另外一個臺電腦IP,本地直接寫localhost即可)
3 再深入了解一下,我們真正開發時肯定是要傳入參數的,例如一般弱聯網游戲通過json來相互交互協議數據等,google查了下最常用的json庫是simplejson。
首先下載安裝simplejson,官網是https://pypi.python.org/pypi/simplejson/?直接下載安裝即可。
?(1)重新修改app/apptest文件
1 #coding:utf8 2 3 from firefly.server.globalobject import webserviceHandle 4 from twisted.web import resource 5 import simplejson as json 6 7 @webserviceHandle('snoy') 8 class helloFirefly(resource.Resource): 9 def render(self, request): 10 length = request.getHeader('content-length') 11 print length 12 version = request.getHeader('Version') 13 print version 14 code = request.code 15 print code 16 method = request.method 17 print method 18 print request.path 19 print request.transport 20 value = request.args 21 jsonData = json.dumps(value) 22 print jsonData 23 s = json.loads(jsonData) 24 print s 25 print s["uid"] 26 27 #test 28 value1 = '{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}' 29 s = json.loads(value1) 30 print s 31 print s.keys() 32 print s["name"] 33 print s["type"]["name"] 34 print s["type"]["parameter"][1] 35 36 return s
?
(2)關閉Firefly 再重新打開
關閉:?pkill -9 -f python*
打開:?python startmaster.py
(瀏覽器輸入http://localhost:1000/snoy?uid=1&passwd=hello&acc=xiuxinming)
注意查看服務器打印內容:
(3)代碼解析:
通過看Twisted文檔知道Twisted技術體系包含2個層次:協議和工廠。協議負責連接成功以后對交互的處理,而工廠則是負責連接過程。在HTTP協議中,連接之后還有個生成HTTP請求報文的過程,所以構造出了一個Request對象來處理具體的一個HTTP請求的報文。在HTTP中的請求報文處理對象是 twisted.web.http.Request 類;HTTP的協議類是 twisted.web.http.HTTPChannel ;HTTP工廠是 twisted.web.http.HTTPFactory類;
這里知道客戶端發給服務器的信息在request里面,那么通過request我們知道所有關于客戶端的信息,其中對我們最主要的就是后面的內容了。
?看打印效果request.args是所以參數的內容,但是看內容格式是字典格式,所以首先通過simplejson把字典轉化為json格式內容然后再讀取出來value;
?基本上到這里對于客戶端的解析部分應該就OK了。再來看下封包的格式,主要是simplejson的使用看simplejson文檔主要就4個方法詳細請 看http://blog.sina.com.cn/s/blog_7ca42bff010185ha.html。
4:基本上如果只是應用的話對于弱社交部分夠使用了以上,但是既然是開源為什么不進去看看到底咋回事呢,雖然一點不懂Twisted和Firefly。。。
反過來追蹤看看Firefly到底做了什么,首先追蹤webserviceHandle在哪里出現,發現是在firefly-master\firefly\firefly\server\globalobject里面,看里面幾個方法有
def masterserviceHandle(target): def netserviceHandle(target): def rootserviceHandle(target): class webserviceHandle:"""這是一個修飾符對象"""def __init__(self,url=None):"""@param url: str http 訪問的路徑"""self._url = urldef __call__(self,cls):""""""if self._url:GlobalObject().webroot.putChild(self._url, cls())else:GlobalObject().webroot.putChild(cls.__name__, cls()) class remoteserviceHandle:
雖然不懂具體是什么意思,但是看名字應該是各種服務的注冊,剛入手從最簡單的開始,看web部分的處理,大概意思應該是注冊的功能。搜索putChild果然出現在Twisted官方文檔里面
def putChild(self, path, child): (source) from twisted.web.resource.IResource Register a static child.You almost certainly don't want '/' in your path. If you intended to have the root of a folder, e.g. /foo/, you want path to be ''.def render(self, request): (source) from twisted.web.resource.IResource overridden in twisted.flow.web.Resource, twisted.web.distrib.ResourceSubscription, twisted.web.error.ErrorPage, twisted.web.proxy.ReverseProxyResource, twisted.web.rewrite.RewriterResource, twisted.web.script.PythonScript, twisted.web.script.ResourceScriptDirectory, twisted.web.script.ResourceScriptWrapper, twisted.web.soap.SOAPPublisher, twisted.web.static.ASISProcessor, twisted.web.static.Data, twisted.web.static.DirectoryLister, twisted.web.static.File, twisted.web.static.Redirect, twisted.web.twcgi.CGIDirectory, twisted.web.twcgi.CGIScript, twisted.web.util.DeferredResource, twisted.web.util.ParentRedirect, twisted.web.util.Redirect, twisted.web.vhost.NameVirtualHost, twisted.web.widgets.Gadget, twisted.web.widgets.Page, twisted.web.widgets.WidgetResource, twisted.web.woven.controller.BlankPage, twisted.web.woven.controller.Controller, twisted.web.woven.form.FormProcessor, twisted.web.woven.guard.SessionWrapper, twisted.web.woven.tapestry.Tapestry, twisted.web.woven.template.DOMController, twisted.web.woven.template.DOMTemplate Render a given resource. See IResource's render method. I delegate to methods of self with the form 'render_METHOD' where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.render_METHOD methods are expected to return a string which will be the rendered page, unless the return value is twisted.web.server.NOT_DONE_YET, in which case it is this class's responsibility to write the results to request.write(data), then call request.finish(). Old code that overrides render() directly is likewise expected to return a string or NOT_DONE_YET.通過閱讀文檔大概了解到resource是一個總節點會往下分發Resource,根據需求可以添加不同的child,render()方法是回調具體處理每一個節點的方法。那基本上有些疑問就解決了,為什么建立好render()方法之后客戶端發來請求會自動調用,為什么注冊一個webserviceHandle傳個參數"snoy"就會自動找到這里來處理。那如果建立多個webserviceHandle傳不同參數會不會都會被調用呢?經過測試時可以的,但是只能建立一級目錄例如只能是http://172.16.3.229:1000/snoy? 而不能是http://172.16.3.229:1000/snoy/test1/test2/ ? 具體原因等以后深入了細看。
再仔細想我們是直接處理的邏輯,那么包頭啊 解析啊 發送等等哪里幫我們處理的呢?還是打開源代碼看到個web文件夾進去看一下只有一個delayrequest.py文件,進去看了下很像是引擎幫我們封裝了細節問題,我們只要處理邏輯就OK了,具體是不是還等待確定。
?
轉載于:https://www.cnblogs.com/liunianing/p/3425865.html
總結
以上是生活随笔為你收集整理的Firefly笔记一之http模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言:代码风格
- 下一篇: 腾讯企业邮箱HTTPS设置