尝试与VPN连接“ x1 = self._socket.sendcmd.encode#Encoding.Default.GetBytescmd +” \ r \ n“OSError:[WinError 10057]

问题描述

x1 = self._socket.send(cmd.encode())#Encoding.Default.GetBytes(cmd +“ \ r \ n”) OSError:[WinError 10057]不允许发送或接收数据的请求,因为未连接套接字,并且(当使用sendto调用在数据报套接字上发送时)未提供地址 班主任: Signal(Enum)类: Hup = 0 期限= 1 1美元= 2 Usr2 = 3

IsConnected = False
_socket = None
bufferSize = 1024
ovpnFileName = ""
eventName = "SISAOpenVpnEvent"
openVpnExePath = ""

def RunopenVpnProcess(self):
    cmd = self.openVpnExePath
    cmd_args = " --config {} --service {} 0".format(self.ovpnFileName,self.eventName)
    cmd_to_run = '"' + cmd + '"' + cmd_args
    try:
        subprocess.Popen(cmd_to_run)
    except Exception as exe:
        print("Subprocess calling--->>>",exe)

def __init__(self,host,port,ovpnFileName,userName=None,password=None,openVpnExeFileName=os.path.join(os.getcwd(),"SISA_VPN\\sisavpn.exe")):
    print("Path---->>",openVpnExeFileName)
    self.openVpnExePath = openVpnExeFileName
    if ovpnFileName != "":
        self.ovpnFileName = ovpnFileName
        if not os.path.isabs(ovpnFileName):
            self.ovpnFileName = os.path.join(os.getcwd(),ovpnFileName)

        # self.RunopenVpnProcess()
        with open(ovpnFileName) as f:
            content = f.readlines()
            # you may also want to remove whitespace characters like `\n` at the end of each line
            content = [x.strip() for x in content]
            ovpnFileContent = content
            print("OVPN Path--->>>",ovpnFileName)
            # management
            management_line = [line for line in ovpnFileContent if "management" in line]
            if management_line:
                idx = ovpnFileContent.index(management_line[0])
                ovpnFileContent[idx] = "management {} {}".format(host,port)
            else:
                lastIdx = len(ovpnFileContent) - 1
                lastLine = ovpnFileContent[lastIdx]
                ovpnFileContent[lastIdx] = "{}{}management {} {}".format(lastLine,'\n',port)

            # auto login
            auth_user_pass_line = [line for line in ovpnFileContent if "auth-user-pass" in line]
            if auth_user_pass_line:
                if userName is None or password is None:
                    raise Exception("Username or password cannot be null")
                passFileName = os.path.join(os.getcwd(),'ovpnpass.txt')
                f = open(passFileName,"w+")
                f.writelines(userName)
                f.writelines('\n')
                f.writelines(password)
                f.close()
                idx = ovpnFileContent.index(auth_user_pass_line[0])
                # add its path the ovpn file and write it back to the ovpn file
                passFileName = passFileName.replace('\\','\\\\')
                ovpnFileContent[idx] = "auth-user-pass {}".format(passFileName)
            else:
                if userName is not None or password is not None:
                    raise Exception(
                        "Username or password are provided but the *.ovpn file doesn't have the line 'auth-user-pass'")

            f = open(ovpnFileName,"w+")
            f.write("\n".join(map(lambda x: str(x),ovpnFileContent)))
            f.close()
            self.RunopenVpnProcess()
    try:
        time.sleep(10)
        self._socket = socket.socket(socket.AF_INET,socket.soCK_STREAM)
        self._socket.connect((host,port))
        # test = self.SendGreeting()
        self.IsConnected = True
    except Exception as exe:
        self.IsConnected = False

def SendGreeting(self):
    # bf = bytes(self.bufferSize)
    rb = self._socket.recv(self.bufferSize)
    if len(rb.decode()) < 1:
        # throw SocketException()
        raise Exception("socket exception")

def SendCommand(self,cmd):
    cmd = cmd + "\r\n"
    x1 = self._socket.send(cmd.encode())  # Encoding.Default.GetBytes(cmd + "\r\n")
    bf = bytes(self.bufferSize)
    sb = ""
    rb = 0
    str = ""
    while True:
        time.sleep(0.1)
        rb = self._socket.recv(self.bufferSize)
        string_data = rb.decode().replace("\0","")
        if len(rb) < len(bf):

            if "\r\nEND" in string_data:
                a = string_data.find("\r\nEND",-1)
                sb += a
            elif "SUCCESS: " in string_data:
                a = string_data.replace("SUCCESS: ","").replace("\r\n","")
                sb += a
            elif "ERROR: " in string_data:
                msg = string_data.replace("ERROR: ","")
                # throw new ArgumentException(msg);
                raise Exception(msg)
            else:
                # todo
                continue
            break

        else:
            sb += string_data
    return sb

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)