httpauth.py 893 B

12345678910111213141516171819202122232425262728293031
  1. """
  2. HTTP basic auth downloader middleware
  3. See documentation in docs/topics/downloader-middleware.rst
  4. """
  5. from w3lib.http import basic_auth_header
  6. from scrapy import signals
  7. class HttpAuthMiddleware(object):
  8. """Set Basic HTTP Authorization header
  9. (http_user and http_pass spider class attributes)"""
  10. @classmethod
  11. def from_crawler(cls, crawler):
  12. o = cls()
  13. crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
  14. return o
  15. def spider_opened(self, spider):
  16. usr = getattr(spider, 'http_user', '')
  17. pwd = getattr(spider, 'http_pass', '')
  18. if usr or pwd:
  19. self.auth = basic_auth_header(usr, pwd)
  20. def process_request(self, request, spider):
  21. auth = getattr(self, 'auth', None)
  22. if auth and b'Authorization' not in request.headers:
  23. request.headers[b'Authorization'] = auth