Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不首先得到401错误的情况下为Python和授权实现机械化_Python_Http_Authentication_Mechanize - Fatal编程技术网

在不首先得到401错误的情况下为Python和授权实现机械化

在不首先得到401错误的情况下为Python和授权实现机械化,python,http,authentication,mechanize,Python,Http,Authentication,Mechanize,我正在尝试使用Mechanize自动化与非常挑剔的遗留系统的交互。特别是,在第一个登录页面之后,必须在每次将您逐出系统的请求中发送授权。不幸的是,Mechanize似乎只满足于在首次收到401未经授权的错误后才发送授权。有没有办法让它每次都发送授权 下面是一些示例代码: br.add_password("http://example.com/securepage", "USERNAME", "PASSWORD", "/MYREALM") br.follow_link(link_to_secure

我正在尝试使用Mechanize自动化与非常挑剔的遗留系统的交互。特别是,在第一个登录页面之后,必须在每次将您逐出系统的请求中发送授权。不幸的是,Mechanize似乎只满足于在首次收到401未经授权的错误后才发送授权。有没有办法让它每次都发送授权

下面是一些示例代码:

br.add_password("http://example.com/securepage", "USERNAME", "PASSWORD", "/MYREALM")
br.follow_link(link_to_secure_page) # where the url is the previous URL
以下是调试Mechanize时得到的响应:

send: 'GET /securepage HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: example.com\r\nReferer: http://example.com/home\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Server: Tandy1000Web
header: Date: Thu, 08 Dec 2011 03:08:04 GMT
header: Connection: close
header: Expires: Tue, 01 Jan 1980 06:00:00 GMT
header: Content-Type: text/html; charset=US-ASCII
header: Content-Length: 210
header: WWW-Authenticate: Basic realm="/MYREALM"
header: Cache-control: no-cache
send: 'GET /securepage HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: example.com\r\nReferer: http://example.com/home\r\nConnection: close\r\nAuthorization: Basic VVNFUk5BTUU6UEFTU1dPUkQ=\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: Tandy1000Web
header: Date: Thu, 08 Dec 2011 03:08:07 GMT
header: Connection: close
header: Last-Modified: Thu, 08 Dec 2011 03:08:06 GMT
header: Expires: Tue, 01 Jan 1980 06:00:00 GMT
header: Content-Type: text/html; charset=UTF-8
header: Content-Length: 33333
header: Cache-control: no-cache
问题在于,与现代web应用程序中使用
GET
请求时应该发生的情况相反,通过先点击401错误,我得到了错误的页面。我已经用CURL和urllib2确认,如果我在第一个请求中通过传入auth头直接命中URL,我将得到正确的页面


关于如何告诉mechanize始终发送auth头并避免第一个401错误的提示?这需要在客户端修复。我无法修改服务器。

你有没有想过?没有。截至撰写此评论(2013年7月底),我尚未找到答案。当我找到答案,或者其他人提供答案时,我会确保接受它。自从我上次问起,我通过添加标题设法解决了这个问题。
from base64 import b64encode
import mechanize

url = 'http://192.168.3.5/table.js'
username = 'admin'
password = 'password'

# I have had to add a carriage return ('%s:%s\n'), but
# you may not have to.
b64login = b64encode('%s:%s' % (username, password))

br = mechanize.Browser()

# # I needed to change to Mozilla for mine, but most do not
# br.addheaders= [('User-agent', 'Mozilla/5.0')]

br.addheaders.append( 
  ('Authorization', 'Basic %s' % b64login )
)

br.open(url)
r = br.response()
data = r.read()

print data