小编msv*_*kon的帖子

如何使用pip在Python3上安装Flask?

我想尝试使用Flask和Python3.我在Ubuntu 14.04上有Python 3.4,据说包含了pip.所以我试过了

pip3 install flask
Run Code Online (Sandbox Code Playgroud)

这结束于:

Cleaning up...
Command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip_build_kramer65/flask/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-i98xjzea-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_kramer65/flask
Storing debug log for failure in /tmp/tmpqc3b2nu5
Run Code Online (Sandbox Code Playgroud)

所以我尝试导入它,但无济于事:

kramer65@vps1:~/cxs$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named …
Run Code Online (Sandbox Code Playgroud)

python pip flask python-3.x

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

如何测试在具有nosetests的函数中调用函数

我正在尝试为项目设置一些自动单元测试.我有一些功能,作为副作用偶尔会调用另一个功能.我想写一个单元测试,测试第二个函数被调用,但我很难过.下面是伪代码示例:

def a(self):
    data = self.get()
    if len(data) > 3500:
        self.b()

    # Bunch of other magic, which is easy to test.

def b(self):
    serial.write("\x00\x01\x02")
Run Code Online (Sandbox Code Playgroud)

我该如何测试b()-gets调用?

python testing unit-testing nosetests

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

gevent StreamServer.start()似乎没有达到我的预期

我试图围绕gevent采用的概念包围我的大脑.这是gevent代码库中的一个示例.这是一个简单的回声服务器.

from gevent.server import StreamServer

# this handler will be run for each incoming connection in a dedicated greenlet
def echo(socket, address):
    print ('New connection from %s:%s' % address)
    socket.sendall('Welcome to the echo server! Type quit to exit.\r\n')
    # using a makefile because we want to use readline()
    fileobj = socket.makefile()
    while True:
        line = fileobj.readline()
        if not line:
            print ("client disconnected")
            break
        if line.strip().lower() == 'quit':
            print ("client quit")
            break
        fileobj.write(line)
        fileobj.flush()
        print ("echoed %r" % line) …
Run Code Online (Sandbox Code Playgroud)

python concurrency networking gevent

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

此事件已被另一个 greenlet 使用

我使用 gunicorn 17.5 with worker_class="gevent", workers=3,我从gunicorn -c config.py my:app. 我刚刚注意到我的日志中有以下错误

2014-04-01 04:48:49 [4297] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/async.py", line 45, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/ggevent.py", line 119, in handle_request
    super(GeventWorker, self).handle_request(*args)
  File "/usr/lib/python2.6/site-packages/gunicorn/workers/async.py", line 93, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/lib/python2.6/site-packages/beaker/middleware.py", line 155, in __call__
    return self.wrap_app(environ, session_start_response)
  [...]
  File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 1083, in cursor
    if not self.is_connected():
  File "/usr/lib/python2.6/site-packages/mysql/connector/connection.py", line 696, in is_connected
    self.cmd_ping()
  File …
Run Code Online (Sandbox Code Playgroud)

python mysql greenlets gunicorn

5
推荐指数
0
解决办法
1306
查看次数

Flask:创建保留在多个请求上的对象

我已经能够创建在此链接的每个请求中创建的对象:http://flask.pocoo.org/docs/appcontext/#locality-of-the-context.

我实际上是在创建一个基于http://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful的API .

我希望能够加载一次对象并让它返回一个已处理的响应,而不是在每次请求时加载它.该对象不是数据库,只需要取消大文件的取消.

我查看了文档,但我仍然对这整个Flask两个状态感到困惑.

python flask flask-restful

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

gevent.StreamServer和非阻塞raw_input()?

我编写了一个简单的tcp服务器gevent.StreamServer用于测试目的.为了让我向一些客户端发送响应,我需要一种非阻塞方式来处理输入raw_input(),最好不使用线程.

经过一些谷歌搜索后,我偶然发现了这个问题:如何在使用eventlet.monkey_patch()时创建非阻塞的raw_input,以及为什么它阻止所有内容,即使在另一个线程上执行?

我写了以下内容,它完全符合我的要求,但我认为有更好的方法.有人能指出我正确的方向吗?此外,理解为什么try/except没有捕获KeyboardInterrupt.

import select
from gevent.monkey import patch_all
from gevent.server import StreamServer

patch_all(os=True, select=True)

def raw_input(message):
    """ Non-blocking input from stdin. """
    sys.stdout.write(message)

    select.select([sys.stdin], [], [])
    return sys.stdin.readline()

def main():
    """ Run the server, listen for commands """

    server = StreamServer(("0.0.0.0", 6000), handle)
    print "Starting server"
    gevent.signal(signal.SIGTERM, server.close)
    gevent.signal(signal.SIGQUIT, server.close)
    gevent.signal(signal.SIGINT, server.close)

    server.start()
    while True:
        try:
            a = raw_input("")
            if a:
                print "Received %s" % a
            gevent.sleep(0)
        except KeyboardInterrupt:
            print "Received a shutdown signal, …
Run Code Online (Sandbox Code Playgroud)

python gevent

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

Gunicorn 和 Supervisor 错误文件没有可执行权限

我正在尝试运行此命令:

sudo supervisorctl start gunicorn_process
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu 上,我收到此错误:

在此处输入图片说明

如您所见,文件“确实”具有可执行权限。

gunicorn_process 文件:

[program:gunicorn_process]
command=/srv/domain wsgi:application
directory=/srv/domain
user=root
Run Code Online (Sandbox Code Playgroud)

python django supervisord gunicorn

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

ASN.1 未对齐 PER 中的二进制补码

我最近不得不使用 ASN.1 未对齐的 PER 编码数据。我在理解数据类型中UPER二进制补码整数编码的方式时遇到问题SEQUENCE

它似乎错误地翻转了最重要的位(单词选择不当)。对于正整数,前导位是1,对于负数,它是0。我认为这里有一种疯狂的方法,但经过一天的工作,我似乎无法从 ITU-T 标准中挖掘出来,也无法自己弄清楚。我怀疑这是因为INTEGER's被包裹在SEQUENCE类型中,但我不明白为什么会这样做。我应该指出,我对 ASN.1 的了解非常有限

一个简单的例子,假设我有以下架构

BEGIN
    FooBar ::= SEQUENCE {
      Foo INTEGER (-512..511),
      Bar INTEGER (-512..511)
    }
END
Run Code Online (Sandbox Code Playgroud)

我正在编码以下内容,如 Unaligned PER

test FooBar ::= 
{
   Foo 10,
   Bar -10 
}
Run Code Online (Sandbox Code Playgroud)

编码结果为十六进制和二进制字符串以及各自的预期值。

HEX:           0x829F60
BIN:           100000101001111101100000

EXPECTED HEX:  0x02BF60
EXPECTED BIN:  000000101011111101100000
Run Code Online (Sandbox Code Playgroud)

关于这里发生了什么的任何想法?

encoding asn.1

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

Python中的可链接动态属性

我想在Python中创建和设置属性属性的值.

这是一个例子:

book = Dynamic()
book.chapter1.section2 = 'abc'
Run Code Online (Sandbox Code Playgroud)

我的book对象没有chapter1属性,因此必须在调用setattr时创建它.我有这个工作:

book = Dynamic()
book.chapter1 = SubDynamic()
book.chapter1.section2 = 'section 2'
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚如何将它连接到一行而不会出现错误,告诉我chapter1不存在.

这就是我所拥有的.

class Dynamic(object):
        def __setattr__(self, name, value):
                sn = SubDynamic(value)
                self.__dict__[name] = v

class SubDynamic(object):
        def __init__(self, attr):
                value = ""
                self.attr = value

        def __setattr__(self, name, value):
                self.__dict__[name] = value

if __name__ == '__main__':
        book = Dynamic()
        book.chapter1.section2 = 'abc'
Run Code Online (Sandbox Code Playgroud)

python

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

我无法在列表中添加int

list == []

def MultiplesNumber(a):
    for i in range(1, a+1):
             if a % i == 0:
                    return i

list.append(MultiplesNumber(100))
TypeError: descriptor 'append' requires a 'list' object but received a 'int'
Run Code Online (Sandbox Code Playgroud)

我不能添加ilist,任何想法?

python data-structures python-3.x python-3.4

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

使用另一个索引列表对列表进行排序

特定

a = [1,4,5,3,2,6,0]
b = ['b','e','f','d','c','g','a']
Run Code Online (Sandbox Code Playgroud)

为了b到位,预期的顺序b是在相应位置元件可用a.

输出将是

['a','b','c','d','e','f','g']
Run Code Online (Sandbox Code Playgroud)

尝试其他类似的输入集.

a = [4,0,1,3,2]
b = ['E','A','B','D','C']
Run Code Online (Sandbox Code Playgroud)

我可以得到它使用第三列表完成,甚至sorted()创建第三列表,但关键是要排序b到位

print sorted(b,key=lambda bi : a[b.index(bi)])
Run Code Online (Sandbox Code Playgroud)

问题的核心是如何防止迭代b已经迭代过的项目.

python sorting algorithm list

0
推荐指数
2
解决办法
112
查看次数