小编Jea*_*one的帖子

尝试使用此代码在TLS上运行TLS时,为什么会出现握手失败?

我尝试使用twisted.protocols.tls一个使用内存BIO的OpenSSL接口来实现一个可以通过TLS运行TLS的协议.

我实现此作为协议封装器的是大多看起来像一个普通的TCP传输,但其具有startTLSstopTLS用于添加和去除分别TLS的层的方法.这适用于第一层TLS.如果我在"原生"Twisted TLS传输上运行它也可以正常工作.但是,如果我尝试使用startTLS此包装器提供的方法添加第二个TLS层,则会立即出现握手错误,并且连接最终会处于某种未知的不可用状态.

包装器和让它工作的两个帮助器看起来像这样:

from twisted.python.components import proxyForInterface
from twisted.internet.error import ConnectionDone
from twisted.internet.interfaces import ITCPTransport, IProtocol
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
from twisted.protocols.policies import ProtocolWrapper, WrappingFactory

class TransportWithoutDisconnection(proxyForInterface(ITCPTransport)):
    """
    A proxy for a normal transport that disables actually closing the connection.
    This is necessary so that when TLSMemoryBIOProtocol notices the SSL EOF it
    doesn't actually close the underlying connection.

    All methods except loseConnection are proxied directly to the real transport.
    """ …
Run Code Online (Sandbox Code Playgroud)

ssl openssl twisted pyopenssl

66
推荐指数
2
解决办法
5132
查看次数

在Python中创建依赖关系图

我继承了一个庞大的代码库,我需要对其进行一些小改动.我想知道是否有实用程序可以解析python代码并在函数之间提供依赖关系,因为如果我对函数进行更改,我想确保我不打破其他函数,所以如果我能在图中看到它如图会让我的生活更轻松

python dependency-management call-flow

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

Pip安装扭曲错误1

pip install Twisted在Mac osx 10.9.4上使用virtualenv时,我得到了这个结果:

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/9r/3b500gbs3093ms87mqqbckr80000gn/T/pip-build-doynftp7/twisted/

我不知道该如何解决.我使用了easy_install,但是我遇到了SSL验证问题.有人有类似的问题吗?其余的错误消息如下.

 Collecting twisted
 Using cached Twisted-16.6.0.tar.bz2
 Complete output from command python setup.py egg_info:
Download error on https://pypi.python.org/simple/incremental/: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) -- Some packages may not be found!
Couldn't find index page for 'incremental' (maybe misspelled?)
Download error on https://pypi.python.org/simple/: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) -- Some packages may not be found!
No local packages or working download links found …
Run Code Online (Sandbox Code Playgroud)

python macos openssl pip osx-mavericks

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

如何在Cython模块中将#defined C值暴露给Python?

我想在这里定义整数常量(ACTIVE_TAG等):

//island management, m_activationState1
#define ACTIVE_TAG 1
#define ISLAND_SLEEPING 2
#define WANTS_DEACTIVATION 3
#define DISABLE_DEACTIVATION 4
#define DISABLE_SIMULATION 5
Run Code Online (Sandbox Code Playgroud)

可用作我正在处理的Cython定义模块的普通属性,以便Python应用程序代码可以访问它们(将它们传递给根据它们定义的包装API).

我已经看过用cdef定义这些作为整数或枚举,但这些方法实际上都没有将值绑定到Cython模块中的属性.还有哪些其他选择?

python cython

25
推荐指数
2
解决办法
6799
查看次数

进程以退出代码139结束(由信号11中断:SIGSEGV)

我正在尝试执行Python脚本,但是我收到以下错误:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)

完整的错误消息可以在这里找到.

我在Linux Mint 18.1 Serena OS上使用python 3.5.2

有人能告诉我为什么会这样,我该怎么解决?

python segmentation-fault linux-mint python-3.5

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

如何在Cython中正确管理C++对象的生命周期?

在为C++库编写Cython包装器时,我遇到了一个不清楚如何正确决定何时删除某些C++实例的情况.

C++库看起来像这样:

#include <stdio.h>
#include <string.h>

class Widget {
    char *name;
    public:
        Widget() : name(strdup("a widget")) {}
        ~Widget() { printf("Widget destruct\n"); }
        void foo() { printf("Widget::foo %s\n", this->name); }
};

class Sprocket {
    private:
        Widget *important;

    public:
        Sprocket(Widget* important) : important(important) {}
        ~Sprocket() { important->foo(); }
};
Run Code Online (Sandbox Code Playgroud)

这个库的一个重要方面是Sprocket析构函数使用了Widget*它,因此Widget必须在它之后才能销毁Sprocket它.

我写的Cython包装器看起来像这样:

cdef extern from "somelib.h":
    cdef cppclass Widget:
        pass

    cdef cppclass Sprocket:
        Sprocket(Widget*)


cdef class PyWidget:
    cdef Widget *thisptr

    def __init__(self):
        self.thisptr = new …
Run Code Online (Sandbox Code Playgroud)

c++ python memory cython

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

如何使用pyasn1解析subjectAltName扩展数据?

我有一些pyOpenSSL给我的数据,'0\r\x82\x0bexample.com'.这应该是subjectAltName X509扩展名的值.我尝试使用pyasn1(并基于其中一个pyasn1示例)为此扩展编码ASN1规范的必要部分:

from pyasn1.type import univ, constraint, char, namedtype

from pyasn1.codec.der.decoder import decode

MAX = 64

class DirectoryString(univ.Choice):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType(
            'teletexString', char.TeletexString().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        namedtype.NamedType(
            'printableString', char.PrintableString().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        namedtype.NamedType(
            'universalString', char.UniversalString().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        namedtype.NamedType(
            'utf8String', char.UTF8String().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        namedtype.NamedType(
            'bmpString', char.BMPString().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        namedtype.NamedType(
            'ia5String', char.IA5String().subtype(
                subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
        )


class AttributeValue(DirectoryString):
    pass


class AttributeType(univ.ObjectIdentifier):
    pass


class AttributeTypeAndValue(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('type', AttributeType()),
        namedtype.NamedType('value', AttributeValue()),
        )


class RelativeDistinguishedName(univ.SetOf):
    componentType = AttributeTypeAndValue()

class RDNSequence(univ.SequenceOf):
    componentType = RelativeDistinguishedName() …
Run Code Online (Sandbox Code Playgroud)

python ssl asn.1 x509

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

如何在使用inlineCallbacks的试验测试用例中使用assertRaises

我正在试图弄清楚如何编写一个试验测试用例,该测试用例会引发异常.

目前我有两种简单的方法来测试(成功和失败).每个方法都返回一个已经回调或错误返回的延迟.测试成功方法很好.在测试失败方法时,我希望能够断言引发了异常(使用assertRaises).

但是测试用例失败了,我得到了:

twisted.trial.unittest.FailTest: ConnectionRefusedError not raised (<Deferred at 0x920e28c current result: <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>> returned)

代码如下:

from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks, succeed, fail
from twisted.internet.error import ConnectionRefusedError

class MyObject:
    def success(self):
        return succeed(True)

    def failure(self):
        return fail(ConnectionRefusedError())


class TestErrBack(TestCase):
    def setUp(self):
        self.o = MyObject()

    @inlineCallbacks
    def test_success(self):
        result = yield self.o.success()
        self.assertTrue(result)

    @inlineCallbacks
    def test_failure(self):
        # this test case is failing !
        yield self.assertRaises(ConnectionRefusedError, self.o.failure)

我在test_failure中使用正确的方法吗?我可以使用try ...来调用self.o.failure,但我不认为这种方法与使用assertRaises一样好.

python twisted trial

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

有哪些策略可以确保在所有语言环境中正确处理所有区域设置感知操作?

有点出于必要,我开发的软件的语言环境设置为"C"或"en_US".使用不同的语言环境很困难,因为我只会说一种语言甚至远程接近流利.

因此,我经常忽略通过使用不同的区域设置可以引入的行为差异.不出所料,忽略这些差异有时会导致错误,这些错误只能由一些使用不同区域设置的不幸用户发现.在特别糟糕的情况下,该用户甚至可能不会与我共享语言,使得错误报告过程变得具有挑战性.而且,重要的是,我的很多软件都是图书馆的形式; 而几乎没有它设置了语言环境,它可能与另一个库相结合,或在其应用程序中使用设置语言环境-产生的行为我从来没有体验自己.

更具体一点,我想到的错误种类并不缺少使用这些本地化的代码中的文本本地化或错误.相反,我的意思是toupper(3)当使用该API的代码没有预料到这种改变的可能性时(例如,在土耳其语语言环境中,toupper不会改变"i ),语言环境会改变某些语言环境感知API(例如)的结果的错误"to"I" - 尝试将特定网络协议讲到另一台主机的网络服务器可能存在问题.

我维护的软件中有一些这样的错误示例:

在过去,我采用的一种处理方法是编写回归测试,明确地将语言环境更改为已知代码无法工作的语言环境,运行代码,验证正确的行为,然后还原原始语言环境.这种方法效果很好,但只有在有人报告了错误之后,它才会覆盖代码库的一小块区域.

另一种似乎可行的方法是建立一个持续集成系统(CIS),以便在具有不同语言环境集的环境中运行一整套测试.通过在测试套件通常给出的一个备用区域设置中提供尽可能多的覆盖,这在一定程度上改善了这种情况.另一个缺点是存在许多很多很多区域设置,并且每个区域设置都可能导致不同的问题.在实践中,区域设置可能只有十几种不同的方式可以破坏程序,但是有几十种额外的测试配置会对资源造成负担(特别是对于已经通过在不同平台上测试,针对不同库来扩展其资源限制的项目版本等).

我想到的另一种方法是使用(可能首先创建)一个新的语言环境,它在各种方式上与"C"语言环境完全不同 - 具有不同的大小写映射,使用不同的千位分隔符,格式化日期不同,此区域设置可以与一个额外的CIS配置一起使用,并希望可以依赖于捕获可由任何区域设置触发的代码中的任何错误.

这样的测试区域是否已经存在?这个想法是否存在缺陷以测试区域设置兼容性?

人们采取了哪些其他的区域测试方法?

我主要对POSIX语言环境感兴趣,因为那些是我所知道的.但是,我知道Windows也有一些类似的功能,因此额外的信息(可能包含有关这些功能如何工作的更多背景信息)也许也很有用.

c python testing locale unit-testing

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

如何以编程方式和并发方式驱动Ansible?

我想使用Ansible在几个远程节点上同时执行一个简单的工作.实际工作涉及点击一些日志文件,然后在我的本地主机(其远程节点上没有软件)上对结果进行后处理.

命令行ansible工具似乎不太适合这种用例,因为它们将ansible生成的格式与远程执行的命令的输出混合在一起.Python API似乎应该能够做到这一点,因为它暴露了未经修改的输出(除了一些潜在的unicode修改,这里不应该相关).

我提出的Python程序的简化版本如下所示:

from sys import argv
import ansible.runner
runner = ansible.runner.Runner(
    pattern='*', forks=10,
    module_name="command",
    module_args=(
        """
        sleep 10
        """),
    inventory=ansible.inventory.Inventory(argv[1]),
)
results = runner.run()
Run Code Online (Sandbox Code Playgroud)

在这里,sleep 10代表实际的日志grepping命令 - 这个想法只是为了模拟一个不会立即完成的命令.

但是,在运行此操作时,我发现所花费的时间似乎与我的库存中的主机数量成正比.以下是分别针对2,5和9主机的库存的时间结果:

exarkun@top:/tmp$ time python howlong.py two-hosts.inventory
real    0m24.285s
user    0m0.216s
sys     0m0.120s
exarkun@top:/tmp$ time python howlong.py five-hosts.inventory                                                                                   
real    0m55.120s
user    0m0.224s
sys     0m0.160s
exarkun@top:/tmp$ time python howlong.py nine-hosts.inventory
real    1m57.272s
user    0m0.360s
sys     0m0.284s
exarkun@top:/tmp$
Run Code Online (Sandbox Code Playgroud)

其他一些随机观察:

  • ansible all --forks=10 -i five-hosts.inventory -m command -a "sleep 10" 表现出相同的行为
  • ansible …

python parallel-processing concurrency ansible

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