You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyScript+GitHub Pages调用群机器人时出现SSL模块不可用错误

问题描述
  • 本地IDE运行Python代码调用钉钉群机器人发送消息功能正常
  • 改用PyScript结合GitHub Pages部署后,抛出SSL错误:

    PythonError: Traceback (most recent call last): (a lot of file names)..(Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))

相关配置与代码:

<py-env>
 - requests
</py-env>
<py-script>
  import requests
  import json

  # Generated sign and timestamp
  
  def send_dingtalk_message(url, content):
      headers = {'wrote-headers'}
      data = {
           "msgtype": "text",
           "text": {
               # Message for the bot to send
               "content": content
           },
       }
      r = requests.post(url, headers=headers, data=json.dumps(data))
      print(r.text)
      return r.text()

  def message(text):
      access_token = 'mainurl&timestamp=' + timestamp +'&sign=' + sign
      content = text
      # Send bot messages
      send_dingtalk_message(access_token, content)
      print("success")
  
  message("text")
</py-script>
解决方案

PyScript基于WebAssembly在浏览器环境运行,标准Python的requests库依赖的SSL模块在此环境下无法正常工作,改用浏览器原生的fetch API即可解决问题,修改后的代码如下:

<py-script>
  import json

  async def send_dingtalk_message(url, content):
      data = {
          "msgtype": "text",
          "text": {
              "content": content
          },
      }
      # 调用浏览器原生fetch发送POST请求
      response = await js.fetch(url, {
          "method": "POST",
          "headers": {
              "Content-Type": "application/json",
              # 替换为你的实际请求头内容
          },
          "body": json.dumps(data)
      })
      result = await response.text()
      print(result)
      return result

  async def message(text):
      access_token = 'mainurl&timestamp=' + timestamp +'&sign=' + sign
      content = text
      await send_dingtalk_message(access_token, content)
      print("success")
  
  await message("text")
</py-script>

注意事项:

  • 由于fetch是异步API,所有调用它的函数需要加上async关键字,调用时需用await
  • 必须设置Content-Type: application/json请求头,符合钉钉机器人的接口要求
  • 替换代码中注释的实际请求头内容

内容的提问来源于stack exchange,提问作者Jerry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:35:14