Python 和 urllib2:如何使用参数发出 GET 请求 [英] Python and urllib2: how to make a GET request with parameters

查看:27
本文介绍了Python 和 urllib2:如何使用参数发出 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个API API",它基本上是一个内部 REST Web 服务的包装器,Web 应用程序将向该服务发出大量请求.一些 Web 服务调用需要 GET 而不是 post,而是传递参数.

I'm building an "API API", it's basically a wrapper for a in house REST web service that the web app will be making a lot of requests to. Some of the web service calls need to be GET rather than post, but passing parameters.

是否有将字典编码为查询字符串的最佳实践"方法?例如:?foo=bar&bla=blah

Is there a "best practice" way to encode a dictionary into a query string? e.g.: ?foo=bar&bla=blah

我正在查看 urllib2 文档,看起来它由本身是否使用 POST 或 GET 基于是否传递参数,但也许有人知道如何将参数字典转换为 GET 请求.

I'm looking at the urllib2 docs, and it looks like it decides by itself wether to use POST or GET based on if you pass params or not, but maybe someone knows how to make it transform the params dictionary into a GET request.

也许外面有一个这样的包?如果它支持 keep-alive 那就太好了,因为 Web 服务器会不断地从 REST 服务请求东西.

Maybe there's a package for something like this out there? It would be great if it supported keep-alive, as the web server will be constantly requesting things from the REST service.

理想情况下,还可以将 XML 转换为某种可遍历的 Python 对象.

Ideally something that would also transform the XML into some kind of traversable python object.

谢谢!

推荐答案

urllib.网址编码

是的,urllib/urllib2 的分工在 Python 2.x 中有点混乱.

And yes, the urllib / urllib2 division of labor is a little confusing in Python 2.x.

这篇关于Python 和 urllib2:如何使用参数发出 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆