婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > ruby實現的一個異步文件下載HttpServer實例

ruby實現的一個異步文件下載HttpServer實例

熱門標簽:巫師3地圖標注魔力之所 外呼線路從哪里出來的 中牟外呼系統違法嗎 天津外呼系統怎么收費 漯河電銷 征服者企業地圖標注 淮安自動外呼系統供應商 AI電銷機器人 線路 柯城手機地圖如何做地圖標注

1.使用ruby eventmachine和em-http-server gem,完成一個簡單的提供文件下載功能的HttpServer

2.使用了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 HttpServerbr>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

您可能感興趣的文章:
  • Ruby中使用多線程隊列(Queue)實現下載博客文章保存到本地文件
  • Ruby中使用mechanize批量下載校內網相冊照片
  • 比較不錯的關于ruby的電子書下載地址集合
  • windows和linux下Ruby的下載與安裝
  • Ruby使用eventmachine為HTTP服務器添加文件下載功能

標簽:西雙版納 甘孜 克拉瑪依 大慶 內江 河池 南昌 棗莊

巨人網絡通訊聲明:本文標題《ruby實現的一個異步文件下載HttpServer實例》,本文關鍵詞  ruby,實現,的,一個,異步,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ruby實現的一個異步文件下載HttpServer實例》相關的同類信息!
  • 本頁收集關于ruby實現的一個異步文件下載HttpServer實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 乌拉特前旗| 石城县| 彭阳县| 东乡| 黄骅市| 望都县| 莱芜市| 滨州市| 当阳市| 成安县| 平顺县| 宾阳县| 南投县| 甘谷县| 肇州县| 嘉义市| 淮滨县| 海伦市| 凤城市| 磐安县| 陕西省| 邵武市| 石首市| 临泉县| 四会市| 获嘉县| 孝感市| 电白县| 香港| 进贤县| 读书| 阳高县| 盐亭县| 重庆市| 汝阳县| 长海县| 宜宾市| 赫章县| 乌兰县| 永和县| 定兴县|