How to specify network interface with Ruby's Faraday?

自闭症网瘾萝莉.ら 提交于 2020-01-05 07:16:11

问题


I'm using Faraday which wraps various adapters, including Net::HTTP, Excon, Typhoeus, Patron, and EventMachine. I would like to specify the network interface (e.g. en0 or en1), but I'm not sure how or if it is possible. I'd be open to answers at the Ruby level or the Linux level.

Note: According to Making HTTP requests from different network interfaces with Ruby and Curb, this is possible with Curb.


回答1:


I think it even is possible to do that with Faraday and Typhoeus: https://github.com/typhoeus/typhoeus/blob/92d19df7af06034ae28996e097676b01301042f5/lib/typhoeus/adapters/faraday.rb#L139.




回答2:


Not sure if it is possible to bind to specific network interface, but with Faraday you can set request option :bind to {:host => "<your_ip>", :port => "<your_port>" } to specify at least IP address and port.

As far as I know em-http, em-synchrony, httpclient & typhoeus adapters respect these settings.




回答3:


I came across this issue with Typhoeus (not using Faraday) and binding to a specific local interface. The following worked for me:

options = {
  interface: '<ip address>'
}
req = Typhoeus::Request.new("https://example.com/3", options)


来源:https://stackoverflow.com/questions/11854660/how-to-specify-network-interface-with-rubys-faraday

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!