鱼C论坛

 找回密码
 立即注册
查看: 1711|回复: 1

[已解决]关于requests库的一个问题

[复制链接]
发表于 2017-7-20 21:54:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from requests.auth import AuthBase

  3. class GithubAuth(AuthBase):
  4.     def __init__(self,token):
  5.         self.token = token

  6.     def __call__(self,r):
  7.         r.headers['Authorization'] = ' '.join(['token',self.token])
  8.         return r


  9. def oauth_advanced():
  10.     auth = GithubAuth('881319f3961ae9ddfgdfgdfgdfgwwercec7447') #这串token我经过处理了的
  11.     response = requests.get('https://api.github.com/user/emails',auth=auth)
  12.     print(response.text)

  13. oauth_advanced()
复制代码



这段代码是可以正常运行的
问题来了:
为什么在__call__上能成功调用参数r,但代码好像并没有传入过参数r
我用debug模式运行发现,r被传入了<PreparedRequest [GET]>,这是什么情况
最佳答案
2017-7-22 16:45:31
D:\Python34\Lib\site-packages\requests\models.py

r = auth(self)  # class PreparedRequest 把自身的实例传进去了
  1.     def prepare_auth(self, auth, url=''):
  2.         """Prepares the given HTTP auth data."""

  3.         # If no Auth is explicitly provided, extract it from the URL first.
  4.         if auth is None:
  5.             url_auth = get_auth_from_url(self.url)
  6.             auth = url_auth if any(url_auth) else None

  7.         if auth:
  8.             if isinstance(auth, tuple) and len(auth) == 2:
  9.                 # special-case basic HTTP auth
  10.                 auth = HTTPBasicAuth(*auth)

  11.             # Allow auth to make its changes.
  12.             r = auth(self)  # class PreparedRequest 把自身的实例传进去了

  13.             # Update self to reflect the auth changes.
  14.             self.__dict__.update(r.__dict__)

  15.             # Recompute Content-Length
  16.             self.prepare_content_length(self.body)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-22 16:45:31 | 显示全部楼层    本楼为最佳答案   
D:\Python34\Lib\site-packages\requests\models.py

r = auth(self)  # class PreparedRequest 把自身的实例传进去了
  1.     def prepare_auth(self, auth, url=''):
  2.         """Prepares the given HTTP auth data."""

  3.         # If no Auth is explicitly provided, extract it from the URL first.
  4.         if auth is None:
  5.             url_auth = get_auth_from_url(self.url)
  6.             auth = url_auth if any(url_auth) else None

  7.         if auth:
  8.             if isinstance(auth, tuple) and len(auth) == 2:
  9.                 # special-case basic HTTP auth
  10.                 auth = HTTPBasicAuth(*auth)

  11.             # Allow auth to make its changes.
  12.             r = auth(self)  # class PreparedRequest 把自身的实例传进去了

  13.             # Update self to reflect the auth changes.
  14.             self.__dict__.update(r.__dict__)

  15.             # Recompute Content-Length
  16.             self.prepare_content_length(self.body)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 21:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表