Python/WebApp Google App Engine - 测试用户/通过 header

标签 python google-app-engine web-applications

当您像这样调用网络服务时:

   username = 'test12'
   password = 'test34' 
   client = httplib2.Http(".cache") 
   client.add_credentials(username,password)
   URL = "http://localhost:8080/wyWebServiceTest"
   response, content = client.request(URL) 

如何将用户名/密码放入服务器端的变量中(即在我正在编写的网络服务中)。 我检查了 self.request.headers 和 self.request.environ 但找不到它们。

(我没有使用 Google 登录,需要根据我自己的数据库反弹此用户 ID/密码以验证安全性。)

我试图从这个页面中获取想法:http://pythonpaste.org/webob/reference.html#headers

谢谢,

尼尔·沃尔特斯

对以下 Peter 的代码进行了轻微改进:

 auth = None 
 if 'Authorization' in self.request.headers: 
    auth = self.request.headers['Authorization']
 if not auth:

最佳答案

我还没有测试过这段代码(插入笑脸),但我认为这是您需要的东西。基本上,如果您的服务器没有将 401 返回给您的客户端,则您的凭据不会出​​现在 header 中(客户端需要知道领域才能知道要提供什么凭据)。

class MYREALM_securepage(webapp.RequestHandler):
  def get(self):
      if not 'Authorization' in self.request.headers:
          self.response.headers['WWW-Authenticate'] = 'Basic realm="MYREALM"'
          self.response.set_status(401)
          self.response.out.write("Authorization required")
      else:
          auth = self.request.headers['Authorization']
          (username, password) = base64.b64decode(auth.split(' ')[1]).split(':')
          # Check the username and password, and proceed ...

关于Python/WebApp Google App Engine - 测试用户/通过 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1499832/

相关文章:

python - Flask 自动重新加载和长时间运行的线程

javascript - 如何使用相同的 Restful 后端在两个独立的网络应用程序(javascript)之间进行通信?

javascript - 使用 HtmlService 透明的 Google App Script 背景

ruby-on-rails - 模拟 Rails/Heroku 中的大量使用

python - 查找较大二维矩阵中存在较小二维矩阵的位置

python - 背景图像来自 Google Drive 中的 plotly

php - App Engine 无法猜测 PHP 文件的 mimetype

python - 如何解码字符串以与 Google 语言检测 API 一起使用?

node.js - 带有 React 和 Node 的谷歌应用引擎 : Production setup

python - 使用 Virtualenv、Circus 和 Chaussette 部署 Django