我正在编写一个小型代理检查实用程序,到目前为止,这是我的代码:
require "thread"
require "socket"
require "http"
require "sinatra"
host = "0.0.0.0"
port = 6660
Thread.new do
class App < Sinatra::Base
set :server, :thin
get '/' do
env.select {|k,v| k.start_with? 'HTTP_'}.collect {|pair| "#{pair[0].sub(/^HTTP_/, '')}: #{pair[1]}"}.join "\n"
end
end
App.run! host: host, port: port
end
sleep 2
queue = Queue.new
ext_ip = Socket.ip_address_list.detect{|intf| intf.ipv4?}.ip_address # and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?}
url = "http://#{url}:#{port}/"
Thread.new do
queue << proxy.split(":") while proxy = gets.chomp
end
servers = [1, 2, 3, 4, 5].map {
Thread.new do
until queue.empty?
p "shit", queue.pop
headers = HTTP.via(*queue.pop).get(url).split("\n").map {|l| l.split(" ", 2)}.to_h
p headers
end
end
}.each do |t|
t.join
end
出于某种原因,这不仅会在localhost:6660上启动sinatra服务器,还会在localhost:4567
上启动sinatra服务器,我绝对不明白为什么。
这是我得到的输出:
== Sinatra (v1.4.6) has taken the stage on 6660 for development with backup from Thin
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:6660, CTRL+C to stop
Stopping ...
== Sinatra has ended his set (crowd applauds)
== Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
第二台服务器来自哪里?当我在浏览器中打开它时,它是默认的Sinatra启动。
答案 0 :(得分:2)
而不是require "sinatra"
,请使用require "sinatra/base"
。只需要sinatra
即可启用内置服务器,该服务器将启动serving the (empty) Sinatra::Application
app in an at_exit
handler。