@@ -3,7 +3,8 @@ Password-Protected Feeds
33
44:program: `Universal Feed Parser ` supports downloading and parsing
55password-protected feeds that are protected by :abbr: `HTTP ( Hypertext Transfer Protocol ) `
6- authentication. Both basic and digest authentication are supported.
6+ basic authentication. For any other types of authentication, you can handle the
7+ authentication yourself and then parse the retrieved feed.
78
89
910Downloading a feed protected by basic authentication (the easy way)
@@ -21,85 +22,19 @@ In this example, the username is test and the password is basic.
2122 >>> d.feed.title
2223 'Sample Feed'
2324
24- The same technique works for digest authentication. (Technically,
25- :program: `Universal Feed Parser ` will attempt basic authentication first, but
26- if that fails and the server indicates that it requires digest authentication,
27- :program: `Universal Feed Parser ` will automatically re-request the feed with
28- the appropriate digest authentication headers. *This means that this technique
29- will send your password to the server in an easily decryptable form. *)
3025
26+ Downloading a feed with other types of authentication
27+ -----------------------------------------------------
3128
32- .. _example.auth.inline.digest :
29+ For any other type of authentication, you should retrieve the feed yourself and
30+ handle authentication as needed (e.g. via `requests
31+ <https://requests.readthedocs.io> ` - this is what :program: `Universal Feed Parser `
32+ uses internally), and then you can just call ``feedparser.parse `` on the
33+ retrieved feed content.
3334
34- Downloading a feed protected by digest authentication (the easy but horribly insecure way)
35- ------------------------------------------------------------------------------------------
3635
37- In this example, the username is test and the password is digest.
38-
39- .. code-block :: pycon
40-
41- >>> import feedparser
42- >>> d = feedparser.parse('http://test:[email protected] /docs/examples/digest_auth.xml') 43- >>> d.feed.title
44- 'Sample Feed'
45-
46-
47-
48- You can also construct a HTTPBasicAuthHandler that contains the password
49- information, then pass that as a handler to the ``parse `` function.
50- HTTPBasicAuthHandler is part of the standard `urllib2 <http://docs.python.org/lib/module-urllib2.html >`_ module.
51-
52- Downloading a feed protected by :abbr: `HTTP ( Hypertext Transfer Protocol ) ` basic authentication (the hard way)
53- --------------------------------------------------------------------------------------------------------------
54-
55- .. code-block :: python
56-
57- import urllib2, feedparser
58-
59- # Construct the authentication handler
60- auth = urllib2.HTTPBasicAuthHandler()
61-
62- # Add password information: realm, host, user, password.
63- # A single handler can contain passwords for multiple sites;
64- # urllib2 will sort out which passwords get sent to which sites
65- # based on the realm and host of the URL you're retrieving
66- auth.add_password(' BasicTest' , ' feedparser.org' , ' test' , ' basic' )
67-
68- # Pass the authentication handler to the feed parser.
69- # handlers is a list because there might be more than one
70- # type of handler (urllib2 defines lots of different ones,
71- # and you can build your own)
72- d = feedparser.parse(
73- ' $READTHEDOCS_CANONICAL_URL/examples/basic_auth.xml' ,
74- handlers = [auth],
75- )
76-
77-
78-
79- Digest authentication is handled in much the same way, by constructing an
80- HTTPDigestAuthHandler and populating it with the necessary realm, host, user,
81- and password information. This is more secure than
82- :ref: `stuffing the username and password in the URL <example.auth.inline.digest >`,
83- since the password will be encrypted before being sent to the server.
84-
85-
86- Downloading a feed protected by :abbr: `HTTP ( Hypertext Transfer Protocol ) ` digest authentication (the secure way)
87- -----------------------------------------------------------------------------------------------------------------
88-
89- .. code-block :: python
90-
91- import urllib2, feedparser
92-
93- auth = urllib2.HTTPDigestAuthHandler()
94- auth.add_password(' DigestTest' , ' feedparser.org' , ' test' , ' digest' )
95- d = feedparser.parse(
96- ' $READTHEDOCS_CANONICAL_URL/examples/digest_auth.xml' ,
97- handlers = [auth],
98- )
99-
100-
101- The examples so far have assumed that you know in advance that the feed is
102- password-protected. But what if you don't know?
36+ Determining that a feed is password-protected
37+ ---------------------------------------------
10338
10439If you try to download a password-protected feed without sending all the proper
10540password information, the server will return an
@@ -113,12 +48,7 @@ you will need to parse it yourself. Everything before the first space is the
11348type of authentication (probably ``Basic `` or ``Digest ``), which controls which
11449type of handler you'll need to construct. The realm name is given as
11550realm="foo" -- so foo would be your first argument to auth.add_password. Other
116- information in the www-authenticate header is probably safe to ignore; the
117- :file: `urllib2 ` module will handle it for you.
118-
119-
120- Determining that a feed is password-protected
121- ---------------------------------------------
51+ information in the www-authenticate header is probably safe to ignore.
12252
12353.. code-block :: pycon
12454
0 commit comments