小编Yan*_*lin的帖子

如何忽略 Apache HttpComponents HttpClient 5.1 中的 SSL 证书错误

如何使用Apache HttpComponents HttpClient 5.1绕过证书验证错误?

我找到了一个可行的解决方案来绕过 HttpClient 4.5 中的此类错误,建议自定义HttpClient实例:

HttpClient httpClient = HttpClients
            .custom()
            .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .build();
Run Code Online (Sandbox Code Playgroud)

但它不适用于 HttpClient 5.1,因为(which returns)中不存在setSSLContext和方法。setSSLHostnameVerifierHttpClientBuilderHttpClients.custom()

java ssl apache-httpcomponents apache-httpclient-5.x

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

如何使用Python @singledispatch 注册typing.Callable?

背景

假设我要实现一个简单的装饰器@notifyme,在调用装饰函数时打印一条消息。我希望装饰器接受一个参数来打印定制消息;参数(以及参数周围的括号)可以省略,在这种情况下将打印默认消息:

@notifyme('Foo is invoked!')
def foo():
    pass

@notifyme  # instead of @notifyme()
def bar():
    pass
Run Code Online (Sandbox Code Playgroud)

为了允许省略括号,我必须提供以下两种实现@notifyme

  1. 第一个实现允许用户自定义消息,因此它接受一个字符串作为参数并返回一个装饰器:

    def notifyme_customized(message: str) -> Callable[[Callable], Callable]:
        def decorator(func: Callable) -> Callable:
            def decorated_func(*args, **kwargs):
                print(str)
                return func(*args, **kwargs)
            return decorated_func
        return decorator
    
    Run Code Online (Sandbox Code Playgroud)
  2. 第二个实现本身是一个装饰器,并使用第一个实现来打印默认消息:

    def notifyme_default(func: Callable) -> Callable:
        return notifyme_customized('The function is invoked.')(func)
    
    Run Code Online (Sandbox Code Playgroud)

为了使上面的两个实现使用相同的名称notifyme,我曾经functools.singledispatch动态地将调用分派notifyme到两个实现之一:

# This is a complete minimal reproducible example

from functools import singledispatch
from typing import Callable …
Run Code Online (Sandbox Code Playgroud)

python callable dynamic-dispatch python-3.8

8
推荐指数
2
解决办法
2411
查看次数

如何在 std::map 中查找具有至少一个数据成员等于键的结构的元素

我有一个关于如何找到相应元素的问题,该元素至少有一个数据编号等于在std::mapwith 结构中搜索的键。

例如,假设我要在电话簿中查找一个人的电话号码及其姓名ID(假设没有一个名字被多个人使用),我将声明一个struct命名Person并定义一个std::map包含Person对象和他的对象/她的电话号码(在 中std::string):

struct Person {
    std::string name;
    std::string id;
};

std::map<Person, std::string> phonebook;
Run Code Online (Sandbox Code Playgroud)

当我从互联网上搜索时,我知道该结构需要重载==<运算符来使用std::map和实现它们:

bool operator==(const Person &person1, const Person &person2) {
    return person1.name == person2.name || person1.id == person2.id;
}

bool operator<(const Person &person1, const Person &person2) {
    return person1.id < person2.id;
}
Run Code Online (Sandbox Code Playgroud)

||在重载==运算符中使用了逻辑“或”( ),以实现可以通过人名ID 找到电话号码的功能,而不是同时需要两者。

我编写了一些代码来测试该功能:

// Add some entries …
Run Code Online (Sandbox Code Playgroud)

c++ struct stdmap

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