soupmatt / persistent_httparty Goto Github PK
View Code? Open in Web Editor NEWPersistent HTTP connections for HTTParty!
License: MIT License
Persistent HTTP connections for HTTParty!
License: MIT License
serv.rb
require "socket"
require 'time'
server = TCPServer.open("127.0.0.1", 80)
loop {
Thread.start(server.accept) do |client| # new thread if connection accepted
puts "connected", client
loop { #for established connection loop gets/write forever
lines = []
while line = client.gets and line !~ /^\s*$/
lines << line.chomp
end
puts lines
puts Thread.current
t = Time.now.utc
echo = "Your conn is serve by thread %s" % Thread.current # echo Thread.current to client
headers = ["HTTP/1.0 200 OK",
"Server: ruby multi thread server",
'Connection: Keep-Alive',
"Content-Type: text/plain; charset=utf-8",
"Date: " << Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT"),
"Age: 267230",
"Content-Length: #{echo.length}\r\n\r\n"].join("\r\n")
puts "%d + %d = %d" % [headers.size , echo.size,headers.size + echo.size]
client.write headers #send to the client
client.write echo
# loop client gets not close conn
}
end
}
client.rb
require "httparty"
require "persistent_httparty"
class Twitter
include HTTParty
persistent_connection_adapter :name => "my_cool_rest_client",
:pool_size => 1,
:idle_timeout => 10,
:keep_alive => 30
base_uri "localhost"
format :html
def self.query
get("/")
end
end
10.times do
resp = Twitter.query
resp.headers.each do |k, v|
print k.capitalize, ": ", v, "\n"
end
puts
puts resp.body
sleep 2
end
```
client recieve different Thread.current ,that means “:pool_size => 1” not use same connection
options = { http_proxyaddr: @proxy_addr, http_proxyport: @proxy_port, http_proxyuser: @proxy_user, http_proxypass: @proxy_pass }
get_response = HTTParty.get(url, options)
The proxy returns random ip whenever call.
But if I use this proxy in browser, it keeps same ip for 15 mins.
So I want to simulate that.
How can I keep connection open and send 2nd request with 1st request ip?
this gem will work for this?
Thanks
Links to httparty and persistent_httparty are interpreted as relative links to files in project, rather than external links to web URLs.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.