小编coo*_*lpy的帖子

Redis - SET 覆盖其他类型

以下代码示例将通过 Python REPL 和 redis-cli 完成/编写。

Redis 服务器 v=2.8.4

背景:将长时间运行的键(哈希)存储在 Redis 键值存储中,然后尝试将另一个键(具有相同名称,但不同类型 - 字符串)存储在同一键值存储中。

首先是代码,然后是问题:

>>> import redis

>>> db = redis.Redis(
...     host='127.0.0.1',
...     port=6379, 
...     password='',
...     db=3)

>>> db.hset("123456", "field1", True)
1

>>> db.type("123456")
b'hash'

>>> db.hgetall("123456")
{b'field1': b'True'}

>>> db.set("123456", "new-value")
True

>>> db.type("123456")
b'string'

>>> db.get("123456")
b'new-value'
Run Code Online (Sandbox Code Playgroud)

您首先会注意到 SET 选项覆盖了 HSET。现在,当我尝试用以下内容覆盖 SET 时:

>>> db.lset("123456", "list1",  "list1value")
Traceback (most recent call last):
  ...
redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value …
Run Code Online (Sandbox Code Playgroud)

redis redis-py

5
推荐指数
1
解决办法
1万
查看次数

Python-Twisted:HTTPS API 的反向代理:无法连接

我正在尝试构建一个反向代理来与某些 API(如 Twitter、Github、Instagram)交谈,然后我可以用我的反向代理调用我想要的任何(客户端)应用程序(把它想象成一个 API 管理器) .

另外,我正在使用 LXC 容器来执行此操作。

例如,这是我从 Twisted Docs 上的示例中破解的最简单的代码:

from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.python.log import startLogging
from sys import stdout
startLogging(stdout)

site = server.Site(proxy.ReverseProxyResource('https://api.github.com/users/defunkt', 443, b''))
reactor.listenTCP(8080, site)
reactor.run()
Run Code Online (Sandbox Code Playgroud)

当我在容器内执行 CURL 时,我得到一个有效的请求(意味着我得到了适当的 JSON 响应)。

这是我如何使用 CURL 命令:

curl https://api.github.com/users/defunkt
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

{
  "login": "defunkt",
  "id": 2,
  "avatar_url": "https://avatars.githubusercontent.com/u/2?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/defunkt",
  "html_url": "https://github.com/defunkt",
  "followers_url": "https://api.github.com/users/defunkt/followers",
  "following_url": "https://api.github.com/users/defunkt/following{/other_user}",
  "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
  "organizations_url": "https://api.github.com/users/defunkt/orgs",
  "repos_url": "https://api.github.com/users/defunkt/repos",
  "events_url": "https://api.github.com/users/defunkt/events{/privacy}",
  "received_events_url": …
Run Code Online (Sandbox Code Playgroud)

python reverse-proxy twisted twisted.web lxc

3
推荐指数
1
解决办法
1913
查看次数

如果具有复合条件的语句,则针对列表中的每个项验证条件

我在这里发现了一个类似的问题,但答案似乎并不适用于我的问题.

这是我的代码:

y = 3
list1 = [1,2,3,4,5]

if y != 0 or y != list1:
    print("y is not in range")
else:
    print(y)
Run Code Online (Sandbox Code Playgroud)

它不断印刷y is not in range.

我的目标是要检查,如果y确实等于0 或者是否y不等于列表中的任何项目.


我理解上面or应该是一个and,我特别感兴趣的是如何检查y列表中包含的条件.

python if-statement python-3.x

1
推荐指数
1
解决办法
80
查看次数