使用Python requests.post调用Auth0 API时遭遇requests.exceptions.SSLError
问题解决:SSL错误(UNKNOWN_PROTOCOL)与HTTP/HTTPS协议不匹配
问题根源
你的开发服务器仅支持HTTP协议,但代码中使用了HTTPS协议向127.0.0.1:8080发送请求,导致SSL握手失败触发SSLError。终端提示的You're accessing the development server over HTTPS, but it only supports HTTP直接点明了问题核心。
修复步骤
- 修正请求URL的协议:把代码里的
url = "https://127.0.0.1:8080/dbconnections/signup"改为url = "http://127.0.0.1:8080/dbconnections/signup" - 移除冗余的SSL验证参数:使用HTTP协议时,
verify=False参数完全没必要,直接删除即可
修改后的代码片段:
@receiver(post_save, sender=OrgUser) def user_is_created(sender, instance, created, **kwargs): if created: full_name = instance.full_name first_name = instance.first_name last_name = instance.last_name email = instance.email cell = instance.cell password = ''.join(random.choices(string.ascii_uppercase + string.digits, k=15)) # 切换为HTTP协议 url = "http://127.0.0.1:8080/dbconnections/signup" user_data = { 'client_id': config("APP_CLIENT_ID"), 'connection': config("DATABASE_CONNECTION"), 'full_name': full_name, 'first_name': first_name, 'last_name': last_name, 'email': email, 'password': password, 'cell': cell, } # 移除verify=False参数 r = requests.post(url, json = user_data, headers = {'User-agent': 'your bot 0.1'}) else: pass
额外说明
- 后续切换到生产环境(支持HTTPS的Auth0服务器)时,再把URL改回HTTPS协议,同时确保
verify=True(默认值,无需显式设置)保障通信安全。 - 终端里的
Bad HTTP/0.9 request type错误是HTTPS请求被HTTP服务器解析导致的乱码,修复协议后该提示会自动消失。
内容的提问来源于stack exchange,提问作者Mike D Hovhannisyan
相关产品推荐
相关产品推荐

