gpt4 book ai didi

python - 从 httplib.HTTP(s)Connection 继承时处理 SSL 和非 SSL 连接

转载 作者:可可西里 更新时间:2023-11-01 17:04:14 24 4
gpt4 key购买 nike

我有一个继承自 httplib.HTTPSConnection 的类。

class MyConnection(httplib.HTTPSConnection):
def __init__(self, *args, **kw):
httplib.HTTPSConnection.__init__(self,*args, **kw)
...

是否可以在实例化类时关闭 SSL 层,以便我也可以使用它与非安全服务器通信?

我的情况是在初始化之前已知是否应该使用 SSL,因此另一种解决方案是尝试将继承从 httplib.HTTPSConnection 切换到 httplib.HTTPConnection,但我也不确定如何以明智的方式做到这一点?

最佳答案

根据你的最后一段,在 Python 中你可以使用类似工厂模式的东西:

class Foo:
def doit(self):
print "I'm a foo"
class Bar:
def doit(self):
print "I'm a bar"

def MakeClass(isSecure):
if isSecure:
base = Foo
else:
base = Bar

class Quux(base):
def __init__(self):
print "I am derived from", base

return Quux()

MakeClass(True).doit()
MakeClass(False).doit()

输出:

I am derived from __main__.Foo
I'm a foo
I am derived from __main__.Bar
I'm a bar

关于python - 从 httplib.HTTP(s)Connection 继承时处理 SSL 和非 SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1282368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com