HTTP-Basic 身份验证上出现 404?

发布于 2024-12-12 11:10:05 字数 1208 浏览 0 评论 0原文

我有以下场景:

  1. 我有一个类似 ShoutCast 的服务器,它提供广播电台 XML 信息。
  2. 我有一个 Flash Player 客户端,需要获取它。
  3. 使用 GET 时,Flash Player 无法通过 HTTP-Basic 身份验证获取内容。
  4. 因此,我正在构建一个 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:

  1. I have a ShoutCast-ish server which serves out radio station XML info.
  2. I have a Flash Player client which needs to fetch it.
  3. Flash Player can't fetch things over HTTP-Basic auth when using GET.
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2024-12-19 11:10:05

它可能还需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文