国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Ruby使用eventmachine為HTTP服務器添加文件下載功能

2019-11-25 16:50:00
字體:
來源:轉載
供稿:網友

思路:
使用ruby eventmachine和em-http-server gem,完成一個簡單的提供文件下載功能的HttpServer;
使用了EM的FileStreamer來異步發送文件,發送文件時先組裝了header,然后調用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 endend

PS:關于eventmachine安裝錯誤的問題
在windows上安裝 eventmachine 總是報錯:
 

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'

后來經過了漫長的Google,找到了2個solution:

1.用更低版本的eventmachine
 
這個提示一直不斷,下面還有一大難錯誤,都是C的編譯錯誤后來網上找了兩個方法
(1)

gem install eventmachine-win32   

這個貌似安裝的是較低版本的
(2)gem install

 eventmachine --pre   

這個貌似安裝的是 beta 1.0.0的。

 
2.升級devkit
 
看了一下,上面沒有提具體的解決方案,但是給出了問題產生的兩個可能原因:
(1)沒有C編譯環境
(2)路徑當中有空格
看看上面的錯誤日志,發現可能就是編譯環境的問題。于是找了一下。
我的ruby是用one-click installer裝的,版本是1.8.6-p398。
在rubyinstaller的addon頁面,找到了DevKit。
看了一下DevKit的說明:

//Sometimes you just want RubyGems to build that cool native,
//C-based extension without squawking.
//Who's your buddy? DevKit!
看來這就是我需要的。
 
出錯的原因是安裝eventmachine的時候,需要build tools,但系統中沒有。出錯信息中同時也給出了解決的法案:
(1) 到 http://rubyinstaller.org/downloads/ 去下載dev kit

主站蜘蛛池模板: 三原县| 丰台区| 四会市| 习水县| 大连市| 晋中市| 交城县| 武乡县| 宁远县| 资讯 | 黔西| 长宁县| 花莲县| 剑川县| 陵水| 安宁市| 咸宁市| 湄潭县| 木兰县| 永胜县| 北辰区| 普兰县| 邯郸市| 福安市| 沿河| 沙湾县| 同江市| 丰镇市| 通辽市| 华坪县| 兰西县| 宁津县| 潢川县| 岳西县| 曲沃县| 临颍县| 镇沅| 舟曲县| 教育| 若尔盖县| 德令哈市|