【发布时间】:2014-08-04 12:31:19
【问题描述】:
我正在尝试使用 XMLRPC 连接到 Wordpress 博客。我正在使用最新的库 v2.3 (http://python-wordpress-xmlrpc.readthedocs.org/en/latest/)。
尝试初始化客户端时出现以下异常:
ServerConnectionError: <ProtocolError for www.myblogaddress.com/xmlrpc.php: 403 Forbidden>
我注意到这是在检查用户名和密码之前发生的,因此它与无效凭据没有任何关系。我相信它可能需要一些自定义标头,例如用户代理,但我不知道如何设置自定义传输参数。
我从 python-wordpress-xmlrpc 库中复制了代码并对其进行了修改,以便进行测试。这是我目前所拥有的:
from xmlrpclib import Transport
class SpecialTransport(Transport):
def send_content(self, connection, request_body):
connection.putheader("Content-Type", "text/xml")
connection.putheader("Content-Length", str(len(request_body)))
connection.putheader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')
connection.putheader('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
connection.putheader('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.3')
connection.putheader('Accept-Encoding','none')
connection.putheader('Accept-Language', 'en-US,en;q=0.8')
connection.putheader('Connection', 'keep-alive')
connection.endheaders()
if request_body:
connection.send(request_body)
url = "{test_url_here}"
try:
server = xmlrpc_client.ServerProxy(url, allow_none=True, transport=SpecialTransport())
supported_methods = server.mt.supportedMethods()
except xmlrpc_client.ProtocolError as err:
print "A protocol error occurred"
print "URL: %s" % err.url
print "HTTP/HTTPS headers: %s" % err.headers
print "Error code: %d" % err.errcode
print "Error message: %s" % err.errmsg
我应该提到我已经从 PHP 脚本成功连接到同一个博客,所以我相信它与 Python 请求有关。知道为什么这不起作用吗?
感谢您的帮助!
【问题讨论】:
标签: python wordpress python-2.7 xml-rpc