我是Ruby的新手,但是我需要创建一个小脚本,它将向服务器发出POST请求,并使用前一个GET请求中的一些参数。我在互联网上找到了一些模板,并试图为自己改变它。但是我并不想工作。这是我的代码:
require 'socksify/http'
useragents = File.readlines('useragents.txt')
uri = URI.parse('http://madrobots.ru/bf/')
TCPSocket::socks_server = "127.0.0.1"
TCPSocket::socks_port = 9050
rubyforge_www = TCPSocket.new("rubyforge.org", 80)
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
useragent = useragents.sample
# get
headers_get = {
'User-Agent' => useragent,
'Referer' => uri
}
res = http.get(uri.path, headers_get)
all_cookies = res.get_fields('set-cookie')
cookies_array = Array.new
all_cookies.each { | cookie |
cookies_array.push(cookie.split('; ')[0])
}
cookies = cookies_array.join('; ')
r = %r{'bitrix_sessid':'(\w+)'}.match res.body
sessid = r && r[1] ? r[1] : ''
# post
data = "SOME_FIELD_1=Y&OTHER_FIELD_2=123&sessid=#{sessid.to_s}"
headers_post = {
'User-Agent' => useragent,
'Referer' => uri,
'Cookie' => cookies
}
res = http.post(uri.path, data, headers_post)
end
当我尝试启动它时,这就是我得到的:
/usr/local/lib/ruby/gems/2.3.0/gems/socksify-1.7.0/lib/socksify.rb:169:in `initialize': Connection refused - connect(2) for "127.0.0.1" port 9050 (Errno::ECONNREFUSED)
from /usr/local/lib/ruby/gems/2.3.0/gems/socksify-1.7.0/lib/socksify.rb:169:in `initialize'
from main.rb:6:in `new'
from main.rb:6:in `<top (required)>'
from /usr/local/lib/ruby/gems/2.3.0/gems/socksify-1.7.0/bin/socksify_ruby:13:in `load'
from /usr/local/lib/ruby/gems/2.3.0/gems/socksify-1.7.0/bin/socksify_ruby:13:in `<top (required)>'
from /usr/local/bin/socksify_ruby:22:in `load'
from /usr/local/bin/socksify_ruby:22:in `<main>'
我是这样开始的:
socksify_ruby localhost 9050 main.rb