HTTP-Basic 身份验证上出现 404?
我有以下场景:
- 我有一个类似 ShoutCast 的服务器,它提供广播电台 XML 信息。
- 我有一个 Flash Player 客户端,需要获取它。
- 使用 GET 时,Flash Player 无法通过 HTTP-Basic 身份验证获取内容。
- 因此,我正在构建一个 Python CGI 脚本来处理身份验证并重新提供信息。
希望这是有道理的。本质上,我的脚本通过创建 HTTP 请求、获取数据并在请求时提供数据来充当真实数据的代理。
这是我的 Python
#!/usr/bin/python
import base64, cgitb, sys, urllib2
cgitb.enable()
print "Content-Type: text/xml"
print
username = "username"
password = "password"
url = "http://s6.voscast.com:7158/admin.cgi?mode=viewxml"
auth = base64.encodestring('%s:%s' % (username, password))[:-1]
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % auth)
try:
handle = urllib2.urlopen(request)
except IOError, e:
print "Something Failed."
sys.exit(1)
print handle.read()
非常简单,对吧?不幸的是,它不起作用。如果我在浏览器中访问该网站并输入相同的用户名和密码,则可以正常工作;我可以看到 XML 树。
相反,我在 stdout
中得到以下输出:
Content-Type: text/xml
ICY 404 Resource Not Found
icy-notice1:<BR>SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-notice2:The resource requested was not found<BR>
我做错了什么?在浏览器中输入相同的 URL、用户名和密码,一切正常。我错过了什么?
I have the following scenario:
- I have a ShoutCast-ish server which serves out radio station XML info.
- I have a Flash Player client which needs to fetch it.
- Flash Player can't fetch things over HTTP-Basic auth when using GET.
- So I'm building a Python CGI script to handle the authentication and re-serve the information.
Hope that makes sense. Essentially, my script serves as a proxy to the real data by creating an HTTP request, fetching the data, and serving it out when requested.
Here's my Python
#!/usr/bin/python
import base64, cgitb, sys, urllib2
cgitb.enable()
print "Content-Type: text/xml"
print
username = "username"
password = "password"
url = "http://s6.voscast.com:7158/admin.cgi?mode=viewxml"
auth = base64.encodestring('%s:%s' % (username, password))[:-1]
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % auth)
try:
handle = urllib2.urlopen(request)
except IOError, e:
print "Something Failed."
sys.exit(1)
print handle.read()
Pretty straightforward, right? Unfortunately, it's not working. If I visit the website in a browser and enter the same username and password, it works; I can see the XML tree.
Instead, I get the following output in stdout
:
Content-Type: text/xml
ICY 404 Resource Not Found
icy-notice1:<BR>SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-notice2:The resource requested was not found<BR>
What am I doing wrong? Given the same URL, username, and password in a browser, everything works. What have I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能还需要 User-Agent 或 Accepts 标头。
尝试使用 Firefox 或 Chrome 开发工具中的 Firebug 或 LiveHTTP 标头来比较/复制浏览器发送的一些 http 标头。比较您的十六进制密码以确认其正确。然后一次添加一个其他标头,直到发现问题为止。
It may require a User-Agent or Accepts header as well.
Try comparing/copying some of the http headers sent by your browser using Firebug or LiveHTTP Headers in Firefox, or Chrome dev tools. Compare your hexed password to confirm it is correct. Then add other headers one at a time until you've discovered the issue.