【问题标题】:Is there any way to match a request with response using Hydra and Typhoeus?有没有办法使用 Hydra 和 Typhoeus 将请求与响应相匹配?
【发布时间】:2014-12-30 17:54:14
【问题描述】:

有没有办法将请求 URL 与该 URL 的响应正文相匹配?

我将 Hydra 用于 GET 请求,并且项目 ID 出现在 URL 中,但不是响应正文,只是有关该项目的详细信息。 100 个 HTTP 请求可以正常发送,但我无法对响应进行排序。

我的替代方法是使用 OpenUri 并一次发送 1 个请求。

products.each do |list|
 url = www.example.com/list.id
 hydra.queue(request)
 request.on_complete do |response|
  if response.body["price"] != list_all.find(request.id?).price
   puts "I can't tell which response goes to which request"
  end
 end
end

解决方案

response.effective_url

【问题讨论】:

    标签: ruby-on-rails ruby typhoeus


    【解决方案1】:

    一切都在那里,但您需要深入研究 Hydra 收到的响应。

    例如:

    require 'typhoeus'
    
    request = Typhoeus::Request.new(
      'www.example.net',
      method: :get,
      headers: { Accept: 'text/html' }
    )
    
    hydra = Typhoeus::Hydra.hydra
    hydra.queue(request)
    hydra.run
    

    这是响应,里面是原始请求:

    response = request.response
    response.request
    # => #<Typhoeus::Request:0x007f9093f9f288
    #     @base_url="www.example.net",
    #     @hydra=
    #      #<Typhoeus::Hydra:0x007f9093f9f0d0
    #       @max_concurrency=200,
    #       @memory={},
    #       @multi=
    #        #<Ethon::Multi:0x007f9093f9f030
    #         @easy_handles=[],
    #         @fd_excep=#<Ethon::Curl::FDSet:0x007f9093f9eba8>,
    #         @fd_read=#<Ethon::Curl::FDSet:0x007f9093f9ed60>,
    #         @fd_write=#<Ethon::Curl::FDSet:0x007f9093f9ec48>,
    #         @handle=#<FFI::AutoPointer address=0x007f9095c74740>,
    #         @max_fd=#<FFI::MemoryPointer address=0x007f9094f484d0 size=4>,
    #         @running_count=0,
    #         @timeout=#<FFI::MemoryPointer address=0x007f9094f3a7e0 size=8>, # !> instance variable @on_body not initialized
    #         @timeval=#<Ethon::Curl::Timeval:0x007f9093f9ee78>>,
    #       @options={},
    #       @queued_requests=[]>,
    #     @on_complete=[],
    #     @on_headers=[],
    #     @on_success=[],
    #     @options=
    #      {:method=>:get,
    #       :headers=>
    #        {"User-Agent"=>"Typhoeus - https://github.com/typhoeus/typhoeus",
    #         :Accept=>"text/html"},
    #       :maxredirs=>50},
    #     @original_options={:method=>:get, :headers=>{:Accept=>"text/html"}},
    #     @response=
    #      #<Typhoeus::Response:0x007f9093f97d08
    #       @options=
    #        {:httpauth_avail=>0,
    #         :total_time=>0.11088,
    #         :starttransfer_time=>0.110587,
    #         :appconnect_time=>0.0,
    #         :pretransfer_time=>0.053747,
    #         :connect_time=>0.053611,
    #         :namelookup_time=>0.001304,
    #         :effective_url=>"HTTP://www.example.net/",
    #         :primary_ip=>"93.184.216.34",
    #         :response_code=>200,
    #         :request_size=>121,
    #         :redirect_count=>0,
    #         :return_code=>:ok,
    #         :response_headers=>
    #          "HTTP/1.1 200 OK\r\nAccept-Ranges: bytes\r\nCache-Control: max-age=604800\r\nContent-Type: text/html\r\nDate: Tue, 30 Dec 2014 19:24:10 GMT\r\nEtag: \"359670651\"\r\nExpires: Tue, 06 Jan 2015 19:24:10 GMT\r\nLast-Modified: Fri, 09 Aug 2013 23:54:35 GMT\r\nServer: ECS (cpm/F9FC)\r\nX-Cache: HIT\r\nx-ec-custom-error: 1\r\nContent-Length: 1270\r\n\r\n",
    #         :response_body=>
    #          "<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <style type=\"text/css\">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 50px;\n        background-color: #fff;\n        border-radius: 1em;\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        body {\n            background-color: #fff;\n        }\n        div {\n            width: auto;\n            margin: 0 auto;\n            border-radius: 0;\n            padding: 1em;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.</p>\n    <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
    #         :debug_info=>
    #          #<Ethon::Easy::DebugInfo:0x007f9093f9de60 @messages=[]>},
    #       @request=#<Typhoeus::Request:0x007f9093f9f288 ...>>>
    

    里面有请求的base_url:

    response.request.base_url
    # => "www.example.net"
    

    还有effective_url

    response.effective_url 
    # => "HTTP://www.example.net/"
    

    如果我没记错的话,第一个是你要求的,第二个是处理重定向后得到的,即你真正到达的地方。

    【讨论】:

    • 谢谢!这就是我想要的 100%
    【解决方案2】:

    你仍然可以访问list 变量,所以你不能从那里得到它吗?喜欢:

      products.each do |list|
        url = www.example.com/list.id
        hydra.queue(request)
        request.on_complete do |response|
          puts "#{list.id}, #{response.body}"
        end
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      相关资源
      最近更新 更多