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
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())
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
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
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))
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))
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")
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())
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))
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))
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))
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())
def encode(path): try: return os.fsencode(path) except AttributeError: return to_bytes(path)
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)
def setopt(self, option, value): self.c.setopt(option, to_bytes(value))
def safequote(url): return quote(to_bytes(url), safe="%/:=&?~#+!$,;'@()*[]")
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()))
def myquote(url): return quote(convert.to_bytes(url, url), safe="%/:=&?~#+!$,;'@()*[]")
def setopt(self, option, value): self.c.setopt(option, convert.to_bytes(value, value))