且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python 中的 urllib2 相当于 ruby

更新时间:2022-04-20 21:10:22

虽然你调用远程服务器就好像它响应 application/x-www-form-urlencoded 数据一样,实际上它只是响应一个命令帖子正文.

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.

在 Python 中,urllib2.urlopen 的数据参数需要一个字符串,它是 application/x-www-form-urlencoded.

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

select * 不是真正的形式编码可以这么说,但无论如何它对你有用,因为服务器将它解释为名为 select * 的查询参数None 值,或者更准确地说,服务器看到带有 select * 的帖子正文并说我知道如何响应该命令".

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".

因此,这里有一个与您正在做的事情等效的 Ruby.

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)