Urllib2 in Python Equivalent for Ruby

urllib2 in python equivalent for ruby

Although you're calling the remote server as if it responds to application/x-www-form-urlencoded data, in fact it's just responding to a command in the post body.

In Python, urllib2.urlopen's data parameter expects a string that's application/x-www-form-urlencoded.

select * isn't really form encoded so to speak, but it's working for you anyway because the server is interpreting it as a query argument named select * with a None value, or more precisely, the server sees a post body with select * and says "I know how to respond to that command".

So, a Ruby equivalent to what you're doing is here.

require 'net/http'
require 'json'

query = "select *"
url = "http://new.openbms.org/backend/api/query"
uri = URI(url)
response = Net::HTTP.post_form(uri, { query => nil })
puts JSON.parse(response.body)

Equivalent ruby code for python

Signature of correspondent method is the same

http = Net::HTTP.new('localhost', 3000)
headers = {'Content-Type' => 'application/json'}
data = {:test => 'test'}

resp, data = http.post(path, data.to_json, headers)

What's the Python equivalent of the Ruby class Net:HTTP?

First: the question is pointless. Every module has lots of functionality. You need to decide what you really need.

Second: use the 'requests' module for Python:

http://python-requests.org/

It's the most Pythonic module for dealing with requests.

different between python and ruby when parsing URL path, which is valid?

Python's urllib is wrong. RFC 3986 Uniform Resource Identifier (URI): Generic Syntax, Section 3.3 Path explicitly gives this exact syntax as an example for a valid path [bold emphasis mine]:

Aside from dot-segments in hierarchical paths, a path segment is considered opaque by the generic syntax. URI producing applications often use the reserved characters allowed in a segment to delimit scheme-specific or dereference-handler-specific subcomponents. For example, the semicolon (";") and equals ("=") reserved characters are often used to delimit parameters and parameter values applicable to that segment. The comma (",") reserved character is often used for similar purposes. For example, one URI producer might use a segment such as "name;v=1.1" to indicate a reference to version 1.1 of "name", whereas another might use a segment such as "name,1.1" to indicate the same. Parameter types may be defined by scheme-specific semantics, but in most cases the syntax of a parameter is specific to the implementation of the URI's dereferencing algorithm.

The correct interpretation of the example URI you posted is the following:

  • scheme = https
  • authority = foo.bar.com
    • userinfo = empty
    • host = foo.bar.com
    • port = empty, derived from the scheme to be 443
  • path = /path/to/aaa.bbb/ccc.ddd;dc_trk_aid=486652617;tfua=;gdpr=;gdpr_consent=, consisting of the following four path segments:
    1. path
    2. to
    3. aaa.bbb
    4. ccc.ddd;dc_trk_aid=486652617;tfua=;gdpr=;gdpr_consent=
  • query = &339286293
  • fragment = empty

What is the Ruby/Rails equivalent of Python's urllib.quote_plus?

CGI::escape does just that:

irb(main):003:0> require 'cgi'
=> true
irb(main):004:0> CGI::escape("foo and/or bar")
=> "foo+and%2For+bar"

Using urllib2 with SOCKS proxy

Try with pycurl:

import pycurl
c1 = pycurl.Curl()
c1.setopt(pycurl.URL, 'http://www.google.com')
c1.setopt(pycurl.PROXY, 'localhost')
c1.setopt(pycurl.PROXYPORT, 8080)
c1.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)

c2 = pycurl.Curl()
c2.setopt(pycurl.URL, 'http://www.yahoo.com')
c2.setopt(pycurl.PROXY, 'localhost')
c2.setopt(pycurl.PROXYPORT, 8081)
c2.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)

c1.perform()
c2.perform()

Python equivalent of Ruby SOCKsify

Try this: http://socksipy.sourceforge.net/

Rails 3.1 urllib2.quote(json.dumps(var)) Equivalent

Try this:

require 'cgi'
require 'json'

cmd2 = [{
'cmd' => 'inlinepush',
'params' => {
'raw' => 'score'
}
}]

puts CGI.escape(JSON.dump(cmd2))


Related Topics



Leave a reply



Submit