使用 rdflib 时如何指定 HTTP auth headers?

How do I specify HTTP auth headers when using rdflib?

docs for rdflib 显示了从远程源加载数据的示例

from rdflib import Graph

g = Graph()
g.parse("http://bigasterisk.com/foaf.rdf")
len(g)
# prints 42

我正在尝试使用需要身份验证的来源来执行此操作 header。我如何告诉 rdflib 使用添加自定义 header 到它的 HTTP 调用?

no way to add extra headers without resorting to fragile hacks. Graph.parse accepts more than URLs, though, so you can use a library like requests 提出请求:

import requests
from rdflib import Graph

response = requests.get('http://bigasterisk.com/foaf.rdf', auth=('user', 'pass'))
response.raise_for_status()  # raise an error on unsuccessful status codes

g = Graph()
g.parse(data=response.text)