開源服務器Tornado的初步了解

文章結束給大家來個程序員笑話:[M]

????
? ? ? ? 明天看了下Python的一個新web框架,由Facebook開源。不得不說,品牌效應啊,只要是Facebook開源的目項,沒有不好用的。Tornado可以說是好用到了極致,從開打官方面頁開始懂得,到搭建一個web服務器,只用了10分鐘。

? ? ? ? 另外,Tornado支撐websocket通信,和天幾前看的通過Netty支撐Websocket比擬,Tornado的作操要顯得單簡得更多。

????下載地址:http://www.tornadoweb.org/en/stable/

????

????安裝步調:

tar xvzf tornado-3.0.1.tar.gz
cd tornado-3.0.1
python setup.py build
sudo python setup.py install

????服務器代碼如下:

import tornado.ioloop
import tornado.web
import tornado.websocketclass MainHandler(tornado.web.RequestHandler):def get(self):self.write("Hello, world")class MainHandler2(tornado.web.RequestHandler):def get(self):self.write("This is a test")class EchoWebSocket(tornado.websocket.WebSocketHandler):def open(self):print "WebSocket opened"def on_message(self, message):self.write_message(u"You said: " + message)def on_close(self):print "WebSocket closed"application = tornado.web.Application([(r"/", MainHandler),(r"/test", MainHandler2),(r"/websocket", EchoWebSocket),
])if __name__ == "__main__":application.listen(8888)tornado.ioloop.IOLoop.instance().start()

????需只要在application中注冊url對應的handler可即,對于websocket,只要讓handler承繼自tornado.websocket.WebSocketHandler。

????

????然后用以下代碼測試:

每日一道理
微笑著,去唱生活的歌謠,不要埋怨生活給予了太多的磨難,不必抱怨生命中有太多的曲折。大海如果失去了巨浪的翻滾,就會失去雄渾;沙漠如果失去了飛沙的狂舞,就會失去壯觀。人生如果僅去求得兩點一線的一帆風順,生命也就失去了存在的意義。
<html><head><title>Web Socket Client</title></head>  
<body>  
<script type="text/javascript">  
var socket;  
if (!window.WebSocket) {  window.WebSocket = window.MozWebSocket;  
}  
// Javascript Websocket Client  
if (window.WebSocket) {  socket = new WebSocket("ws://localhost:8888/websocket");  socket.onmessage = function(event) {  var ta = document.getElementById('responseText');  ta.value = ta.value + '\n' + event.data  };  socket.onopen = function(event) {  var ta = document.getElementById('responseText');  ta.value = "Web Socket opened!";  };  socket.onclose = function(event) {  var ta = document.getElementById('responseText');  ta.value = ta.value + "Web Socket closed";  };  
} else {  alert("Your browser does not support Web Socket.");  
}  
// Send Websocket data  
function send(message) {  if (!window.WebSocket) { return; }  if (socket.readyState == WebSocket.OPEN) {  socket.send(message);  } else {  alert("The socket is not open.");  }  
}  
</script>  
<h3>Send :</h3>  
<form οnsubmit="return false;">  
<input type="text" name="message" value="Hello World!"/><input type="button" value="Send Web Socket Data" οnclick="send(this.form.message.value)" />  
<h3>Receive :</h3>  
<textarea id="responseText" style="width:500px;height:300px;"></textarea>  
</form>  
</body>  
</html>

????效果如下:

????開源和服務器

????

????

文章結束給大家分享下程序員的一些笑話語錄: 開發時間
  項目經理: 如果我再給你一個人,那可以什么時候可以完工?程序員: 3個月吧!項目經理: 那給兩個呢?程序員: 1個月吧!
項目經理: 那100呢?程序員: 1年吧!
項目經理: 那10000呢?程序員: 那我將永遠無法完成任務.

轉載于:https://www.cnblogs.com/jiangu66/archive/2013/05/07/3065681.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274263.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274263.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274263.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

開發人員安全行為規則_為開發人員改善生活的7條簡單規則

開發人員安全行為規則As people, we have a superpower; it is the power of perception. As the famous saying goes, life is 10% what happens to us and 90% how we respond to it.作為人&#xff0c;我們擁有超級大國&#xff1b; 這是感知的力量。 俗話說&#xff0c;生活…

多線程的那群“象”

最初學習多線程的時候&#xff0c;只學了用Thread這個類&#xff0c;記憶中也用過Mutex&#xff0c;到后來只記得Thread的使用&#xff0c;其余的都忘了。知道前不久寫那個Socket連接池時遇到了一些對象如&#xff1a;Semaphore&#xff0c;Interlocked&#xff0c;Mutex等&…

反饋頁面設計_獲得設計反饋的藝術

反饋頁面設計I’m going to assume that you already know the importance of sharing work early and often to get feedback from your team and stakeholders. And how it’s such a critical part of the design process. Cool. Glad we’re on the same page.我假設您已經…

轉 求結構體偏移

原文出處忘記了。。。 此宏很神奇 #define FIND(structTest,e) (size_t)&(((structTest*)0)->e) struct test { int a; int b; char c; int d; }; size_t s FIND(test,a); cout<<s<<endl; //輸出結構體偏移轉載于:https://www.cnblogs.com/zhangdongsheng…

系統設計原則的重要性_設計原則的重要性及其對好的設計的影響

系統設計原則的重要性The principles of design are the most important part of any design process. Without these principles, it would be very difficult for the users to understand the type of message that the designer is trying communicate.設計原則是任何設計過…

優秀HTML5網站學習范例:從“饑餓游戲瀏覽器”談用戶體驗

繼影片《饑餓游戲》獲得票房成功后&#xff0c;《饑餓游戲2&#xff1a;火星燎原》也于2012年宣布開拍&#xff0c;將在今年的11月22日登陸全球各大院線。值此之際&#xff0c;微軟攜手美國獅門影業公司和 RED Interactive Agency 一起為影迷打造了一個基于 HTML5 現代網頁規范…

有一種愛情叫永不改變_設計就像愛情一樣,總是在尋找一種方式

有一種愛情叫永不改變If you’re a designer, you know what I’m talking about. You just pitched some amazing brand extension ideas to your client and the worst thing happened. They killed the very best one, the one you have a ton of heart for. You know it ca…

linux網絡編程系列-select和epoll的區別

select和epoll屬于I/O多路復用模型&#xff0c;用于持續監聽多個socket&#xff0c;獲取其IO事件。 select&#xff08;輪詢&#xff09; 該模型輪詢各socket&#xff0c;不管socket是否活躍&#xff0c;隨著socket數的增加&#xff0c;性能逐漸下降。 #include <sys/select…

產品原型制作_早期制作原型如何幫助您設計出色的數字產品

產品原型制作Utilizing prototypes this way, is a missed shot. A missed shot to create an outcome that solves a real problem for customers. An outcome that is worthwhile. Prototypes are a great tool to discover what people need, what they dream of, what thei…

ThinkPHP add、save無法添加、修改不起作用

ThinkPHP add、save無法添加、修改不起作用 案例&#xff1a;數據庫新添加一字段&#xff0c;修改值不成功。解決方案&#xff1a;將Runtime/Data/_fields/下面的字段緩存刪除&#xff0c;或者直接刪除整個Runtime文件也是可以的分析&#xff1a;由于Thinkphp&#xff0c;采用字…

photoshop最新版本_iPad Pro應該擁有更好的Photoshop版本

photoshop最新版本I remember when Adobe came to an Apple Keynote in 2018 to show how powerful was Photoshop on the new iPad Pros.我記得Adobe在2018年參加Apple Keynote時展示了Photoshop在新iPad Pro上的強大功能。 In fact, like everyone else, I was blown away, …

android 輔助功能_關于輔助功能的9個神話

android 輔助功能Most designers don’t know about accessibility or have misconceptions about it, such as thinking it will hinder their creativity or that it doesn’t apply to their clients. Find out 9 myths about accessibility and why you and your clients s…

【語言處理與Python】1.5自動理解自然語言

【詞義消歧】在詞義消歧中&#xff0c;我們要算出特定上下文中詞被賦予的是哪個意思。自動消除歧義需要使用上下文&#xff0c;利用相鄰詞匯有相近含義這樣一個簡單的事實。【指代消解】解決“誰對誰做了什么”&#xff0c;即監測主語和動詞的賓語。確定帶刺或名詞短語指的是什…

絆倒在舌頭上

It’s not something you’d confess in tenth grade, but I’ve always been fascinated by typography. By crisp, pared down symbols in clear white space, the infinite variety of lines, ligatures and curves that make up the letters of language. Even the sample …

[開源]jquery.ellipsis根據寬度(不是字數)進行內容截斷,支持多行內容

jquery.ellipsis 自動計算內容寬度&#xff08;不是字數&#xff09;截斷&#xff0c;并加上省略號&#xff0c;內容不受中英文或符號限制。 如果根據字數來計算的話&#xff0c;因為不同字符的寬度并不相同&#xff0c;比如l和W&#xff0c;特別是中英文&#xff0c;最終內容寬…

前端開發時間格式的轉換方法_開發人員投資時間而不浪費時間的10種方法

前端開發時間格式的轉換方法In today’s, in the past and probably in the future world — the time is more valuable than money, and the right time waits for no one. Hence, we have to make the most out of it to succeed in life.在當今&#xff0c;過去甚至未來世界…

鏈表基本操作

單鏈表結構&#xff1a; typedef struct node { int data; struct node *next; }node&#xff1b; typedef struct node *LinkList; /*創建單鏈表&#xff0c;將新的節點插入到鏈表的尾部*/ createList(LinkList L, int n) { LinkList p,r;  //p節點用來接收插入的元素&#…

python 投資組合_重新設計投資組合的好處

python 投資組合Yep, I’m here to nag you a bit about that portfolio that you haven’t updated in a while.是的&#xff0c;我在這里想和您談談您有一段時間沒有更新的作品集。 Yes, it’s time to get to work on it again.是的 &#xff0c;是時候重新開始研究了。 Y…

李安的電影夢by李安

1978年,當我準備報考美國伊利諾大學的戲劇電影系時&#xff0c;父親十分反感&#xff0c;他給我舉了一個數字&#xff1a;在美國百老匯&#xff0c;每年只有200個角&#xff0c;但卻有50000人要一起爭奪這少得可憐的角色。當時我一意孤行&#xff0c;決意登上了去美國的班機&am…

抓取html中用到的css_如何使用HTML和CSS制作像《星球大戰》一樣的抓取文字

抓取html中用到的cssThe opening to Star Wars is iconic. The effect of text scrolling both up and away from the screen was both a crazy cool special effect for a movie back in 1977 and a cool typographical style that was brand new at the time.《星球大戰》的開…