gpt4 book ai didi

python - 带有 https 和 http 身份验证的 pywikipedia bot

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:22 24 4
gpt4 key购买 nike

我无法让我的机器人登录到 Intranet 上的 MediaWiki 安装。我相信这是由于保护 wiki 的 http 身份验证。

事实:

  1. 维基根是:https://local.example.com/mywiki/
  2. 当使用网络浏览器访问 wiki 时,弹出窗口要求提供企业凭据(我假设这是基本访问身份验证)

这是我的 user-config.py 中的内容:

mylang = 'en'
family = 'mywiki'
usernames['mywiki']['en'] = u'Bot'
authenticate['local.example.com'] = ('user', 'pass')

这是我在 mywiki_family.py 中的内容:

# -*- coding: utf-8  -*-
import family, config

# The Wikimedia family that is known as mywiki
class Family(family.Family):
def __init__(self):
family.Family.__init__(self)
self.name = 'mywiki'
self.langs = { 'en' : 'local.example.com'}

def scriptpath(self, code):
return '/mywiki'

def version(self, code):
return '1.13.5'

def isPublic(self):
return False

def hostname(self, code):
return 'local.example.com'

def protocol(self, code):
return 'https'

def path(self, code):
return '/mywiki/index.php'

当我执行 login.py -v -v 时,我得到:

urllib2.urlopen(urllib2.Request('https://local.example.com/w/index.php?title=Special:Userlogin&useskin=monobook&action=submit', wpSkipCookieCheck=1&wpPassword=XXXX&wpDomain=&wpRemember=1&wpLoginattempt=Aanmelden%20%26%20Inschrijven&wpName=Bot, {'Content-type': 'application/x-www-form-urlencoded', 'User-agent': 'PythonWikipediaBot/1.0'})):
(Redundant traceback info here)
urllib2.HTTPError: HTTP Error 401: Unauthorized

(我不确定为什么它有“local.example.com/w”而不是“/mywiki”。)

我认为它可能会尝试向 example.com 而不是 example.com/wiki 进行身份验证,因此我将身份验证行更改为:

authenticate['local.example.com/mywiki'] = ('user', 'pass')

但随后我从 IIS 返回 HTTP 401.2 错误:

You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.

如能提供有关如何实现此功能的任何帮助,我们将不胜感激。

更新 修复我的家庭文件后,它现在显示:

Getting information for site mywiki:en ('http error', 401, 'Unauthorized', ) WARNING: Could not open 'https://local.example.com/mywiki/index.php?title=Non-existing_page&action=edit&useskin=monobook'. Maybe the server or your connection is down. Retrying in 1 minutes...

我查看了计划 urllib2.ulropen 调用中的 HTTP header ,它使用的是 WWW-Authenticate: Negotiate WWW-Authenticate: NTLM。我猜 urllib2 和 pywikipedia 不支持这个?

更新 添加了一个可口的赏金以帮助实现它。我可以使用 python-ntlm 进行身份验证。我如何将它集成到 pywikipedia 中?

最佳答案

login.py 尝试访问“\w”而不是您的路径这一事实表明存在系列配置问题。

您的代码缩进很奇怪:scriptpath 是新 Family 类的成员吗?如:

class Family(family.Family):
def __init__(self):
family.Family.__init__(self)
self.name = 'mywiki'
self.langs = { 'en' : 'local.example.com'}

def scriptpath(self, code):
return '/mywiki'

def version(self, code):
return '1.13.5'

def isPublic(self):
return False

def hostname(self, code):
return 'local.example.com'

def protocol(self, code):
return 'https'

?

我认为您的家庭文件有问题。检查的好方法是在 python 控制台中执行:

import wikipedia
site = wikipedia.getSite('en', 'mywiki')
print site.login_address()

只要相对地址错误,显示的是'/w'而不是'/mywiki',就说明family文件还是配置不正确,bot不会工作:)

更新:如何在pywikipedia中集成ntlm?

我刚刚看了基本示例 here .我会将代码集成到 login.py 中的该行之前:

response = urllib2.urlopen(urllib2.Request(self.site.protocol() + '://' + self.site.hostname() + address, data, headers))

你想写类似的东西:

from ntlm import HTTPNtlmAuthHandler

user = 'DOMAIN\User'
password = "Password"
url = self.site.protocol() + '://' + self.site.hostname()

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

response = urllib2.urlopen(urllib2.Request(self.site.protocol() + '://' + self.site.hostname() + address, data, headers))

如果我有一个可用的 ntlm 设置,我会测试它并将它直接集成到 pywikipedia 代码库中......

无论发生什么,请不要随您的解决方案一起消失:在 pywikipedia,我们对您的解决方案很感兴趣 :)

关于python - 带有 https 和 http 身份验证的 pywikipedia bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1256213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com