Skip to main content
 首页 » 编程设计

python之IronPython 中 urllib 的 PROTOCOL_TLS 错误

2025年01月19日8zhujiabin

我正在使用由 twitch 机器人加载的 python 脚本。不幸的是,该机器人不包含 if then else 的逻辑处理程序,但幸好有 python 脚本支持来处理机器人外部的逻辑。

在导入 urllib 后使用 urllib.urlopen 时返回此错误:

消息:未定义全局名称“PROTOCOL_TLS”

我已经在 IDE 中构建了机器人外部的代码,并且代码执行良好,因此 IronPython 而非常规 Python 似乎需要做一些特殊的事情来处理调用。

整个脚本文件包含返回到集成的附加信息,但整个文件是:

#--------------------------------------- 
# Import Libraries 
#--------------------------------------- 
 
import os 
import urllib 
 
#--------------------------------------- 
#         Version History 
#--------------------------------------- 
# 1.0.0.0 Initial Release. 
 
#--------------------------------------- 
# Variables 
#--------------------------------------- 
SettingsFile = os.path.join(os.path.dirname(__file__), "SCShoutoutSettings.json") 
 
#--------------------------------------- 
# Classes 
#--------------------------------------- 
class Settings(object): 
    def __init__(self, settingsfile=None): 
        try: 
            with codecs.open(settingsfile, encoding="utf-8-sig", mode="r")  
as f: 
                self.__dict__ = json.load(f, encoding="utf-8") 
        except: 
            self.Enabled = True 
            self.Command = "!so" 
            self.CD_Time = 0 
            self.Permissions = "Moderator" 
            self.Fee = False 
            self.Cost = 0 
 
    def Reload(self, jsondata): 
        self.__dict__ = json.loads(jsondata, encoding="utf-8") 
    return 
 
def Save(self, settingsfile): 
    try: 
        with codecs.open(settingsfile, encoding="utf-8-sig", mode="w+") as f: 
            json.dump(self.__dict__, f, encoding="utf-8", ensure_ascii=False) 
        with codecs.open(settingsfile.replace("json", "js"), encoding="utf-8-sig", mode="w+") as f: 
            f.write("var settings = {0};".format(json.dumps(self.__dict__, encoding='utf-8', ensure_ascii=False))) 
    except: 
        Parent.Log(ScriptName, "Failed to save settings to file.") 
    return 
 
#--------------------------------------- 
# Initialize Data on Load 
#--------------------------------------- 
def Init(): 
    global ScriptSettings 
    ScriptSettings = Settings(SettingsFile) 
    return 
 
#--------------------------------------- 
# Reload Settings on Save 
#--------------------------------------- 
def ReloadSettings(jsondata): 
    ScriptSettings.Reload(jsondata) 
    return 
 
#--------------------------------------- 
#    Script is going to be unloaded 
#--------------------------------------- 
def Unload(): 
    ScriptSettings.Save(SettingsFile) 
    return 
 
#--------------------------------------- 
#    Script is enabled or disabled on UI 
#--------------------------------------- 
def ScriptToggled(state): 
    if not state: 
        ScriptSettings.Save(SettingsFile) 
    return 
 
#--------------------------------------- 
# Execute data and process messages 
#--------------------------------------- 
 def Execute(data): 
 
    if data.IsChatMessage(): 
        global ScriptSettings 
        if data.GetParam(0).lower() == ScriptSettings.Command: 
        target = data.GetParam(1) 
        gameLink = "http://customapi.deepbot.tv/streamgame/" + target 
        followLink = "http://customapi.deepbot.tv/totalfollows/" + target 
        twitchLink = "https://twitch.tv/" + target 
        gameSock = urllib.urlopen(gameLink) 
        followSock = urllib.urlopen(followLink) 
        game = gameSock.read() 
        gameSock.close() 
        follow = followSock.read() 
        followSock.close() 
        if game == "Error reaching Twitch API.": 
            message = target + " is not a streamer" 
        else: 
            message = "Check out " + target + " the last game they were playing was " + game + " they have " + follow + " followers, why not add some more?  Go to " + twitchLink 
 
        Parent.SendStreamMessage(message) 
return 
 
#--------------------------------------- 
# Tick 
#--------------------------------------- 
def Tick(): 
    return 
 
#--------------------------------------- 
# SetDefaults Custom User Interface Button 
#--------------------------------------- 
def SetDefaults(): 
 
    # Globals 
    global ScriptSettings 
 
    # Set defaults by not supplying a settings file 
    ScriptSettings = Settings() 
 
    # Save defaults back to file 
    ScriptSettings.Save(SettingsFile) 
 
    # End of SetDefaults 
    return 

请您参考如下方法:

这是由 IronPython 中的一个问题引起的,IronPython 是 streamlabs 聊天机器人中使用的 python 版本(是的,我知道你在谈论那个 ^^)。这是 urllib 使用的 ssl 模块中的问题。这也是为什么聊天机器人预见到以下功能的原因:

https://github.com/AnkhHeart/Streamlabs-Chatbot-Python-Boilerplate/wiki/API-Requests

我正在寻找自己的解决方案,但到目前为止,我不得不重写所有内容才能使用这些功能。这也是为什么 urllib2、requests 和许多其他模块无法工作的原因。