0

I'm having problems with a web scraper in Ruby. I'm using Mechanize gem.

Is there any way to get the local port used in the agent connection? It would be very useful for my problem.

For instance, in TCPSockets, I can call:

2.3.3 :029 > t = TCPSocket.new
 => #<TCPSocket:fd 14> 
2.3.3 :030 > t.addr
 => ["AF_INET", 45164, "127.0.0.1", "127.0.0.1"] 

Here, 45164 is the port number I need.

I'd like to do something like

 agent = Mechanize.new
 page = agent.get "http://www.google.com/"

 agent.addr

and get the local port number.

Regards.

user2553863
  • 682
  • 1
  • 8
  • 17
  • Maybe something like `agent.instance_variable_get('@agent').instance_variable_get('@http').instance_variable_get('@pool').instance_variable_get('@available').instance_variable_get('@ques').values.first.first.instance_variable_get('@http').local_port`? – anothermh Apr 24 '19 at 18:39
  • Thanks, @anothermh, but it returned nil. Did it work for you? – user2553863 Apr 25 '19 at 11:43
  • I meant that you should use that as a jumping off point for digging into mechanize’s guts. – anothermh Apr 25 '19 at 16:28
  • Yes, I think I did! Using your hint, I wrote a recursive method that explored all instance variables and methods looking for either a port value or an addr or address method. Didn't find what I need. I think I'll try to redefine some of the socksify methods, that use TCPSocket, and try to get it from there... I'm not sure how exactly, but I'll try that. Thank you very much. – user2553863 Apr 25 '19 at 23:29

0 Answers0