Python 中的 CURL 替代方案 [英] CURL alternative in Python

查看:25
本文介绍了Python 中的 CURL 替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 PHP 中使用的 cURL 调用:

I have a cURL call that I use in PHP:

curl -i -H 'Accept: application/xml' -u login:key "https://app.streamend.com/emails"

curl -i -H 'Accept: application/xml' -u login:key "https://app.streamsend.com/emails"

我需要一种在 Python 中做同样事情的方法.Python 中是否有 cURL 的替代方法.我知道 urllib,但我是 Python 菜鸟,不知道如何使用它.

I need a way to do the same thing in Python. Is there an alternative to cURL in Python. I know of urllib but I'm a Python noob and have no idea how to use it.

推荐答案

import urllib2

manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, 'https://app.streamsend.com/emails', 'login', 'key')
handler = urllib2.HTTPBasicAuthHandler(manager)

director = urllib2.OpenerDirector()
director.add_handler(handler)

req = urllib2.Request('https://app.streamsend.com/emails', headers = {'Accept' : 'application/xml'})

result = director.open(req)
# result.read() will contain the data
# result.info() will contain the HTTP headers

# To get say the content-length header
length = result.info()['Content-Length']

您的 cURL 调用使用 urllib2.完全未经测试.

Your cURL call using urllib2 instead. Completely untested.

这篇关于Python 中的 CURL 替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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