Web Based Helpers¶
-
download
(url, save_to_file=True, save_dir='.', filename=None, block_size=64000, overwrite=False, quiet=False)¶ Download a given URL to either file or memory
Parameters: - url – Full url (with protocol) of path to download
- save_to_file – boolean if it should be saved to file or not
- save_dir – location of saved file, default is current working dir
- filename – filename to save as
- block_size – download chunk size
- overwrite – overwrite file if it already exists
- quiet – boolean to turn off logging for function
Returns: save location (or content if not saved to file)
-
class
ThreadedServer
(name='', port=8080, auto_start=True, server=<class 'http.server.HTTPServer'>, handler=<class 'http.server.SimpleHTTPRequestHandler'>)¶ Defaulting as a FileServer, this class allows for fast creation of a threaded server that is easily stoppable.
my_server = reusables.ThreadedServer() reusables.download("http://localhost:8080", False) # '...<html><title>Directory listing for /</title>...' my_server.stop()
Parameters: - name – server name
- port – int of port to run server on (below 1024 requires root)
- auto_start – automatically start the background thread and serve
- server – Default is TCPServer (py2) or HTTPServer (py3)
- handler – Default is SimpleHTTPRequestHandler
-
start
()¶ Create a background thread for httpd and serve ‘forever’
-
stop
()¶ Stop the httpd server and join the thread
-
url_to_ip
(url)¶ Provide IP of host, does not support IPv6, uses socket.gethostbyaddr
reusables.url_to_ip('example.com') # '93.184.216.34'
Parameters: url – hostname to resolve to IP addresses Returns: string of IP address or None
-
url_to_ips
(url, port=None, ipv6=False, connect_type=<SocketKind.SOCK_STREAM: 1>, proto=6, flags=0)¶ Provide a list of IP addresses, uses socket.getaddrinfo
reusables.url_to_ips("example.com", ipv6=True) # ['2606:2800:220:1:248:1893:25c8:1946']
Parameters: - url – hostname to resolve to IP addresses
- port – port to send to getaddrinfo
- ipv6 – Return IPv6 address if True, otherwise IPv4
- connect_type – defaults to STREAM connection, can be 0 for all
- proto – defaults to TCP, can be 0 for all
- flags – additional flags to pass
Returns: list of resolved IPs
-
ip_to_url
(ip_addr)¶ Resolve a hostname based off an IP address.
This is very limited and will probably not return any results if it is a shared IP address or an address with improperly setup DNS records.
reusables.ip_to_url('93.184.216.34') # example.com # None reusables.ip_to_url('8.8.8.8') # 'google-public-dns-a.google.com'
Parameters: ip_addr – IP address to resolve to hostname Returns: string of hostname or None