Esempio n. 1
0
def safeurlencode(data):
    if isiterable(data):
        res = urlencode(
            dict((to_bytes(x), to_bytes(y)) for x, y in dict(data).items()))
    else:
        res = urlencode(to_bytes(data))
    return res
Esempio n. 2
0
def safeurlencode(data):
    if isiterable(data):
        res = urlencode(
            dict((to_bytes(x), to_bytes(y)) for x, y in dict(data).items()))
    else:
        res = urlencode(to_bytes(data))
    return res
Esempio n. 3
0
    def set_request_context(self,
                            url,
                            get,
                            post,
                            referer,
                            cookies,
                            multipart=False):
        """
        Sets everything needed for the request.
        """
        url = myquote(url)

        if get:
            get = urlencode(get)
            url = "{0}?{1}".format(url, get)

        self.c.setopt(pycurl.URL, url)

        if post:
            self.c.setopt(pycurl.POST, 1)
            if not multipart:
                if isinstance(post, str):
                    post = convert.to_bytes(post, post)
                elif not isinstance(post, bytes):
                    post = myurlencode(post)

                self.c.setopt(pycurl.POSTFIELDS, post)
            else:
                post = [(x, convert.to_bytes(y, y)) for x, y in post.items()]
                self.c.setopt(pycurl.HTTPPOST, post)
        else:
            self.c.setopt(pycurl.POST, 0)

        if referer and self.last_url:
            self.headers['Referer'] = str(self.last_url)
        else:
            self.headers['Referer'] = ""

        if cookies:
            for c in self.cj.output().splitlines():
                self.c.setopt(pycurl.COOKIELIST, c)
        else:
            # Magic string that erases all cookies
            self.c.setopt(pycurl.COOKIELIST, b"ALL")

        # TODO: remove auth again
        if "auth" in self.options:
            auth = self.options['auth']
            self.c.setopt(pycurl.USERPWD, convert.to_bytes(auth, auth))

        self.c.setopt(pycurl.HTTPHEADER, self.headers.list())
Esempio n. 4
0
    def change_password(self, user, oldpw, newpw):
        self.c.execute('SELECT rowid, name, password FROM users WHERE name=?',
                       (user, ))
        r = self.c.fetchone()
        if not r:
            return False

        hashed_pw = r[2]
        if bcrypt.checkpw(to_bytes(oldpw), hashed_pw):
            new_hpw = bcrypt.hashpw(to_bytes(newpw), bcrypt.gensalt())
            self.c.execute('UPDATE users SET password=? WHERE name=?',
                           (new_hpw, user))
            return True

        return False
Esempio n. 5
0
    def change_password(self, user, oldpw, newpw):
        self.c.execute(
            'SELECT rowid, name, password FROM users WHERE name=?', (user,))
        r = self.c.fetchone()
        if not r:
            return False

        hashed_pw = r[2]
        if bcrypt.checkpw(to_bytes(oldpw), hashed_pw):
            new_hpw = bcrypt.hashpw(to_bytes(newpw), bcrypt.gensalt())
            self.c.execute(
                'UPDATE users SET password=? WHERE name=?', (new_hpw, user))
            return True

        return False
Esempio n. 6
0
def set_console_title(value):
    title = convert.to_bytes(value)
    if os.name == 'nt':
        import ctypes
        ctypes.windll.kernel32.SetConsoleTitleA(title)
    else:
        sys.stdout.write('\x1b]2;{0}\x07'.format(title))
Esempio n. 7
0
def set_console_title(value):
    title = convert.to_bytes(value, value)
    if os.name == 'nt':
        import ctypes
        ctypes.windll.kernel32.SetConsoleTitleA(title)
    else:
        stdout.write("\x1b]2;{0}\x07".format(title))
Esempio n. 8
0
    def set_request_context(self, url, get, post,
                            referer, cookies, multipart=False):
        """
        Sets everything needed for the request.
        """
        url = myquote(url)

        if get:
            get = urlencode(get)
            url = "{0}?{1}".format(url, get)

        self.c.setopt(pycurl.URL, url)

        if post:
            self.c.setopt(pycurl.POST, 1)
            if not multipart:
                if isinstance(post, str):
                    post = convert.to_bytes(post, post)
                elif not isinstance(post, bytes):
                    post = myurlencode(post)

                self.c.setopt(pycurl.POSTFIELDS, post)
            else:
                post = [(x, convert.to_bytes(y, y))
                        for x, y in post.items()]
                self.c.setopt(pycurl.HTTPPOST, post)
        else:
            self.c.setopt(pycurl.POST, 0)

        if referer and self.last_url:
            self.headers['Referer'] = str(self.last_url)
        else:
            self.headers['Referer'] = ""

        if cookies:
            for c in self.cj.output().splitlines():
                self.c.setopt(pycurl.COOKIELIST, c)
        else:
            # Magic string that erases all cookies
            self.c.setopt(pycurl.COOKIELIST, b"ALL")

        # TODO: remove auth again
        if "auth" in self.options:
            auth = self.options['auth']
            self.c.setopt(pycurl.USERPWD, convert.to_bytes(auth, auth))

        self.c.setopt(pycurl.HTTPHEADER, self.headers.list())
Esempio n. 9
0
def set_console_title(value):
    title = convert.to_bytes(value)
    if os.name == "nt":
        import ctypes

        ctypes.windll.kernel32.SetConsoleTitleA(title)
    else:
        sys.stdout.write(f"\x1b]2;{title}\x07")
Esempio n. 10
0
    def set_request_context(self,
                            url,
                            get,
                            post,
                            referer,
                            cookies,
                            multipart=False):
        """Sets everything needed for the request."""
        self.rep = io.StringIO()
        url = safequote(url)

        if get:
            get = urlencode(get)
            url = '{0}?{1}'.format(url, get)

        self.setopt(pycurl.URL, url)

        if post:
            self.setopt(pycurl.POST, 1)
            if not multipart:
                post = safeurlencode(post)
                self.setopt(pycurl.POSTFIELDS, post)
            else:
                post = [(x, to_bytes(y)) for x, y in post.items()]
                self.setopt(pycurl.HTTPPOST, post)
        else:
            self.setopt(pycurl.POST, 0)

        if referer and self.last_url:
            self.headers['Referer'] = to_bytes(self.last_url)
        else:
            self.headers['Referer'] = b''

        if cookies:
            for c in self.cj.output().splitlines():
                self.setopt(pycurl.COOKIELIST, c)
        else:
            # Magic string that erases all cookies
            self.setopt(pycurl.COOKIELIST, 'ALL')

        # TODO: remove auth again
        if 'auth' in self.options:
            self.setopt(pycurl.USERPWD, self.options['auth'])

        self.setopt(pycurl.HTTPHEADER, self.headers.list())
Esempio n. 11
0
    def set_interface(self, options):

        interface, proxy, ipv6 = options[
            'interface'], options['proxies'], options['ipv6']

        if interface and interface.lower() != "none":
            self.c.setopt(
                pycurl.INTERFACE,
                convert.to_bytes(
                    interface,
                    interface))

        if proxy:
            if proxy['type'] == "socks4":
                self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS4)
            elif proxy['type'] == "socks5":
                self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
            else:
                self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP)

            host = proxy['host']
            self.c.setopt(pycurl.PROXY, convert.to_bytes(host, host))
            self.c.setopt(pycurl.PROXYPORT, proxy['port'])

            if proxy['username']:
                userpwd = "{0}:{1}".format(proxy['username'], proxy['password'])
                self.c.setopt(
                    pycurl.PROXYUSERPWD,
                    convert.to_bytes(
                        userpwd,
                        userpwd))

        if ipv6:
            self.c.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_WHATEVER)
        else:
            self.c.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4)

        if "timeout" in options:
            self.c.setopt(pycurl.LOW_SPEED_TIME, options['timeout'])

        if "auth" in options:
            auth = self.options['auth']
            self.c.setopt(pycurl.USERPWD, convert.to_bytes(auth, auth))
Esempio n. 12
0
    def add_user(self, user, password, role, permission):
        hashed_pw = bcrypt.hashpw(to_bytes(password), bcrypt.gensalt())

        self.c.execute('SELECT name FROM users WHERE name=?', (user,))
        if self.c.fetchone() is not None:
            self.c.execute(
                'UPDATE users SET password=?, role=?, permission=? '
                'WHERE name=?', (hashed_pw, role, permission, user))
        else:
            self.c.execute(
                'INSERT INTO users (name, role, permission, password) '
                'VALUES (?, ?, ?, ?)', (user, role, permission, hashed_pw))
Esempio n. 13
0
    def add_user(self, user, password, role, permission):
        hashed_pw = bcrypt.hashpw(to_bytes(password), bcrypt.gensalt())

        self.c.execute('SELECT name FROM users WHERE name=?', (user, ))
        if self.c.fetchone() is not None:
            self.c.execute(
                'UPDATE users SET password=?, role=?, permission=? '
                'WHERE name=?', (hashed_pw, role, permission, user))
        else:
            self.c.execute(
                'INSERT INTO users (name, role, permission, password) '
                'VALUES (?, ?, ?, ?)', (user, role, permission, hashed_pw))
Esempio n. 14
0
    def set_request_context(self, url, get, post,
                            referer, cookies, multipart=False):
        """Sets everything needed for the request."""
        self.rep = io.BytesIO()
        url = safequote(url)

        if get:
            get = urlencode(get)
            url = '{0}?{1}'.format(url, get)

        self.setopt(pycurl.URL, url)

        if post:
            self.setopt(pycurl.POST, 1)
            if not multipart:
                post = safeurlencode(post)
                self.setopt(pycurl.POSTFIELDS, post)
            else:
                post = [(x, to_bytes(y))
                        for x, y in post.items()]
                self.setopt(pycurl.HTTPPOST, post)
        else:
            self.setopt(pycurl.POST, 0)

        if referer and self.last_url:
            self.headers['Referer'] = self.last_url
        else:
            self.headers['Referer'] = ''

        if cookies:
            for c in self.cj.output().splitlines():
                self.setopt(pycurl.COOKIELIST, c)
        else:
            # Magic string that erases all cookies
            self.setopt(pycurl.COOKIELIST, 'ALL')

        # TODO: remove auth again
        if 'auth' in self.options:
            self.setopt(pycurl.USERPWD, self.options['auth'])

        self.setopt(pycurl.HTTPHEADER, self.headers.list())
Esempio n. 15
0
File: fs.py Progetto: pyblub/pyload
def encode(path):
    try:
        return os.fsencode(path)
    except AttributeError:
        return to_bytes(path)
Esempio n. 16
0
def encode(path):
    try:
        return os.fsencode(path)
    except AttributeError:
        return to_bytes(path)
Esempio n. 17
0
def exec_cmd(command, *args, **kwargs):
    cmd = shlex.split(command)
    cmd.extend(convert.to_bytes(x, x) for x in args)
    xargs = {'bufsize': -1, 'stdout': PIPE, 'stderr': PIPE}
    xargs.update(kwargs)
    return Popen(cmd, **xargs)
Esempio n. 18
0
 def setopt(self, option, value):
     self.c.setopt(option, to_bytes(value))
Esempio n. 19
0
def safequote(url):
    return quote(to_bytes(url), safe="%/:=&?~#+!$,;'@()*[]")
Esempio n. 20
0
def safequote(url):
    return quote(to_bytes(url), safe="%/:=&?~#+!$,;'@()*[]")
Esempio n. 21
0
def myurlencode(data):
    data = dict(data)
    return urlencode(
        dict((convert.to_bytes(x, x), convert.to_bytes(y, y))
             for x, y in data.items()))
Esempio n. 22
0
def myquote(url):
    return quote(convert.to_bytes(url, url), safe="%/:=&?~#+!$,;'@()*[]")
Esempio n. 23
0
 def setopt(self, option, value):
     self.c.setopt(option, convert.to_bytes(value, value))
Esempio n. 24
0
def myurlencode(data):
    data = dict(data)
    return urlencode(
        dict((convert.to_bytes(x, x), convert.to_bytes(y, y))
             for x, y in data.items()))
Esempio n. 25
0
def myquote(url):
    return quote(convert.to_bytes(url, url), safe="%/:=&?~#+!$,;'@()*[]")