IronPython中urllib的PROTOCOL_TLS错误

时间:2018-03-13 17:51:23

标签: python urllib ironpython

我正在使用由twitch bot加载的python脚本。不幸的是,机器人不包含if if 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

1 个答案:

答案 0 :(得分:1)

这是由IronPython中的一个问题引起的,IronPython是streamlabs chatbot中使用的python版本(是的,我知道您在谈论那个^^)。 urllib使用的ssl模块中存在问题。这也是chatbot具备以下功能的原因:

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

我自己在寻找解决方案,但是到目前为止,我不得不重写所有内容以使用这些功能。这就是urllib2,请求和许多其他模块不起作用的相同原因。