我正在尝试访问使用Symfony框架设计的本地网站。 它与Web浏览器和CURL完美配合,但是当我使用Mechanize时,我总是得到401服务器的未授权答案。
import mechanize
# Browser
br = mechanize.Browser()
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)
# Does not change anything even if we change thos
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
# Here is my website
r = br.open('http://localhost:8080/frontend_dev.php/home')
html = r.read()
# Show the html source
print html
你知道为什么会这样吗?
由于
答案 0 :(得分:6)
我终于找到了自己的答案。 sfDoctrineGuardPlugin(和sfGuardPlugin)中存在一个错误,导致此行为如here所述。 我应用了补丁,问题解决了。
答案 1 :(得分:1)
对谁可以访问symfony中的开发环境有严格的控制。尝试使用主URL(可能是http://localhost:8080/index.php/home)
答案 2 :(得分:1)
您可以参考一些有效的地址来试用您的代码吗? 我猜测您错过了一些设置,尝试禁用机器人。
br.set_handle_robots(False)
答案 3 :(得分:1)
我对Symfony框架一无所知,但是当你得到'Error : 401 '
时。添加这些行可能会有效。
br.add_password('http://localhost:8080/frontend_dev.php/home', 'username', 'password')
br.open('http://localhost:8080/frontend_dev.php/home')
答案 4 :(得分:1)