Ruby使用eventmachine為HTTP服務(wù)器添加文件下載功能
思路:
使用ruby eventmachine和em-http-server gem,完成一個(gè)簡(jiǎn)單的提供文件下載功能的HttpServer;
使用了EM的FileStreamer來(lái)異步發(fā)送文件,發(fā)送文件時(shí)先組裝了header,然后調(diào)用FileStreamer。
代碼:
require 'rubygems' require 'eventmachine' require 'em-http-server' class HTTPHandler < EM::HttpServer::Server attr_accessor :filename, :filesize, :path def process_http_request #send file async if @http_request_method.to_s =~ /GET/ && @http_request_uri.to_s.end_with?(filename) send_data "HTTP/1.1 200 OK\n" send_data "Server: XiaoMi\n" send_data "Connection: Keep-Alive\n" send_data "Keep-Alive: timeout=15\n" send_data "Content-Type: application/octet-stream\n" send_data "Content-Disposition: filename='#{filename}'\n" send_data "Content-Length: #{filesize}\n" send_data "\n" streamer = EventMachine::FileStreamer.new(self, path) streamer.callback { # file was sent successfully close_connection_after_writing } else response = EM::DelegatedHttpResponse.new(self) response.status = 200 response.content_type 'text/html' response.content = "Package HttpServer<br>usage: wget http://host:port/#{filename}" response.send_response end end end EM::run do path = '/tmp/aaa.tar.gz' EM::start_server("0.0.0.0", 8080, HTTPHandler) do |conn| conn.filename = File.basename(path) conn.filesize = File.size(path) conn.path = path end end
PS:關(guān)于eventmachine安裝錯(cuò)誤的問(wèn)題
在windows上安裝 eventmachine 總是報(bào)錯(cuò):
Building native extensions. This could take a while... ERROR: Error installing eventmachine: ERROR: Failed to build gem native extension.
或者另外一種:
ERROR: Error installing ruby-debug: The 'linecache' native gem requires installed build tools. Please update your PATH to include build tools or download the DevKit from 'http://rubyinstaller.org/downloads' and follow the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
后來(lái)經(jīng)過(guò)了漫長(zhǎng)的Google,找到了2個(gè)solution:
1.用更低版本的eventmachine
這個(gè)提示一直不斷,下面還有一大難錯(cuò)誤,都是C的編譯錯(cuò)誤后來(lái)網(wǎng)上找了兩個(gè)方法
(1)
gem install eventmachine-win32
這個(gè)貌似安裝的是較低版本的
(2)gem install
eventmachine --pre
這個(gè)貌似安裝的是 beta 1.0.0的。
2.升級(jí)devkit
看了一下,上面沒(méi)有提具體的解決方案,但是給出了問(wèn)題產(chǎn)生的兩個(gè)可能原因:
(1)沒(méi)有C編譯環(huán)境
(2)路徑當(dāng)中有空格
看看上面的錯(cuò)誤日志,發(fā)現(xiàn)可能就是編譯環(huán)境的問(wèn)題。于是找了一下。
我的ruby是用one-click installer裝的,版本是1.8.6-p398。
在rubyinstaller的addon頁(yè)面,找到了DevKit。
看了一下DevKit的說(shuō)明:
//Sometimes you just want RubyGems to build that cool native,
//C-based extension without squawking.
//Who's your buddy? DevKit!
看來(lái)這就是我需要的。
出錯(cuò)的原因是安裝eventmachine的時(shí)候,需要build tools,但系統(tǒng)中沒(méi)有。出錯(cuò)信息中同時(shí)也給出了解決的法案:
(1) 到 http://rubyinstaller.org/downloads/ 去下載dev kit – DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe
(2)按照 http://github.com/oneclick/rubyinstaller/wiki/Development-Kit/ 安裝dev kit
主要安裝步驟如下:
如果原來(lái)系統(tǒng)中已經(jīng)安裝了舊版的dev kit, 則刪除它
下載上面提到的dev kit
解壓下載下來(lái)的文件到指定的目錄,如c:/devkit。(注意:目錄不能有空格)
運(yùn)行ruby dk.rb,然后按照提示分別運(yùn)行ruby dk.rb init 和 ruby dk.rb install來(lái)增強(qiáng)ruby
可以運(yùn)行
gem install rdiscount –platform=ruby
來(lái)測(cè)試是否成功。
按照安裝步驟,完成了DevKit的安裝,非常簡(jiǎn)單。
然后,再次安裝eventmachine:
gem install eventmachine
相關(guān)文章
一鍵搞定python連接mysql驅(qū)動(dòng)有關(guān)問(wèn)題(windows版本)
這篇文章主要介紹了對(duì)于mysql驅(qū)動(dòng)問(wèn)題折騰了一下午,現(xiàn)共享出解決方案,需要的朋友可以參考下2016-04-04用Python自動(dòng)清理電腦內(nèi)重復(fù)文件,只要10行代碼(自動(dòng)腳本)
這篇文章主要介紹了用Python自動(dòng)清理電腦內(nèi)重復(fù)文件,只要10行代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01基于Python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)WechatPCAPI來(lái)實(shí)現(xiàn)微信自動(dòng)回復(fù)的功能,文中的示例代碼講解詳細(xì),快跟隨小編一起動(dòng)手嘗試一下2022-06-06Opencv實(shí)現(xiàn)鼠標(biāo)事件與窗口互動(dòng)功能過(guò)程
平時(shí)在做圖像處理demo或者研究測(cè)試算法時(shí),經(jīng)常會(huì)用到imshow和鼠標(biāo)的交互,比如在顯示圖像的窗口上畫(huà)點(diǎn)、線、圓、矩形、多邊形等操作,故在此做出用法總結(jié)2022-12-12pandas is in和not in的使用說(shuō)明
這篇文章主要介紹了pandas is in和not in的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03