简体   繁体   English

如何在Python中实现curl -u?

[英]How to implement curl -u in Python?

I am trying to use http://developer.github.com/v3/ to retrieve project issues.我正在尝试使用http://developer.github.com/v3/来检索项目问题。 This works:这有效:

curl -u "Littlemaple:mypassword" https://api.github.com/repos/MyClient/project/issues

It returns all private issues of my client's project.它返回我客户项目的所有私人问题。 However, I am not able to find out how to implement this in Python.但是,我无法找到如何在 Python 中实现这一点。 Both ways I have found (eg Python urllib2 Basic Auth Problem ) doesn't work, they return 404 or 403 errors:我发现的两种方法(例如Python urllib2 Basic Auth Problem )都不起作用,它们返回 404 或 403 错误:

def fetch(url, username, password):
    """Wonderful method found on forums which does not work.""""
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, url, username, password)
    urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))

    req = urllib2.Request(url)
    f = urllib2.urlopen(req)
    return f.read()

...and: ...和:

def fetch(url, username, password):
    """Wonderful method found on forums which does not work neither.""""
    request = urllib2.Request(url)
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)   
    return urllib2.urlopen(request).read()

Any ideas?有任何想法吗? Thanks in advance!提前致谢!

r = requests.get('https://api.github.com', auth=('user', 'pass'))

Python requests is the way to go here. Python requests是 go 的方式。 I've been using requests extensively at work and at home for various web service interactions.我一直在工作和家里广泛使用requests来进行各种 web 服务交互。 It is a joy to use compared to what came before it.与之前的产品相比,使用它是一种乐趣。 Note: the auth keyword arg works on any call that requires auth.注意: auth关键字 arg 适用于任何需要 auth 的调用。 Thus, you can use it sparingly, ie you don't need it for every call against GitHub, only those that require logins.因此,您可以谨慎使用它,即您不需要在每次针对 GitHub 的调用中使用它,只需要那些需要登录的调用。 For instance:例如:

r = requests.get('https://api.github.com/gists/starred', auth=('user', 'pass'))

The GitHub login is documented here: GitHub 登录记录在此处:

http://pypi.python.org/pypi/requests/0.6.1 http://pypi.python.org/pypi/requests/0.6.1

If it's 404, you probably just have the wrong URL.如果是 404,您可能只是输入了错误的 URL。 If it's 403, perhaps you have the realm wrong.如果是 403,则可能是 realm 错误。

For starters, you're passing the URL to add_password, when in fact you should only be passing the base URL.对于初学者,您将 URL 传递给 add_password,而实际上您应该只传递基本 URL。 Also, instead of install_opener, you should probably just create a new opener.此外,您可能应该只创建一个新的开启程序,而不是 install_opener。

See this recipe for an example :有关示例,请参见此配方

class NoOpHandler(urllib2.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newUrl):
        return None

passmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmanager.add_password(None, baseurl, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(passmanager)
opener = urllib2.build_opener(auth_handler, NoOpHandler())

You can also do it this way你也可以这样做

 r = requests.get('https://user:pass@api.github.com')

Use pycurl which is python interface to libcurl .使用pycurl ,它是libcurl的 python 接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM