Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer import of requests and urllib #1906

Merged
merged 3 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The table below shows which release corresponds to each branch, and what date th
- [#1893][1893] Fix bytes warning in "pwn cyclic"
- [#1903][1903] Add zsh completion script
- [#1904][1904] Add bash completion script
- [#1906][1906] Defer import of several modules to save on startup time

[1733]: https://github.com/Gallopsled/pwntools/pull/1733
[1876]: https://github.com/Gallopsled/pwntools/pull/1876
Expand All @@ -76,6 +77,7 @@ The table below shows which release corresponds to each branch, and what date th
[1893]: https://github.com/Gallopsled/pwntools/pull/1893
[1903]: https://github.com/Gallopsled/pwntools/pull/1903
[1904]: https://github.com/Gallopsled/pwntools/pull/1904
[1906]: https://github.com/Gallopsled/pwntools/pull/1906

## 4.6.0 (`beta`)

Expand Down
1 change: 0 additions & 1 deletion pwn/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import platform
import re
import requests
import socks
import signal
import string
Expand Down
10 changes: 7 additions & 3 deletions pwnlib/libcdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import codecs
import json
import os
import requests
import tempfile

from six.moves import urllib

from pwnlib.context import context
from pwnlib.elf import ELF
from pwnlib.log import getLogger
Expand All @@ -29,6 +26,10 @@
# https://gitlab.com/libcdb/libcdb wasn't updated after 2019,
# but still is a massive database of older libc binaries.
def provider_libcdb(hex_encoded_id, hash_type):
# Deferred import because it's slow
import requests
from six.moves import urllib

# Build the URL using the requested hash type
url_base = "https://gitlab.com/libcdb/libcdb/raw/master/hashes/%s/" % hash_type
url = urllib.parse.urljoin(url_base, hex_encoded_id)
Expand All @@ -53,6 +54,9 @@ def provider_libcdb(hex_encoded_id, hash_type):

# https://libc.rip/
def provider_libc_rip(hex_encoded_id, hash_type):
# Deferred import because it's slow
import requests

# Build the request for the hash type
# https://github.com/niklasb/libc-database/blob/master/searchengine/api.yml
if hash_type == 'build_id':
Expand Down
4 changes: 3 additions & 1 deletion pwnlib/tubes/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import socket
import socks
import ssl as _ssl

from pwnlib.log import getLogger
from pwnlib.timeout import Timeout
Expand Down Expand Up @@ -85,6 +84,9 @@ def __init__(self, host, port,
self.lhost, self.lport = self.sock.getsockname()[:2]

if ssl:
# Deferred import to save startup time
import ssl as _ssl

ssl_args = ssl_args or {}
ssl_context = ssl_context or _ssl.SSLContext(_ssl.PROTOCOL_TLSv1_2)
if isinstance(sni, str):
Expand Down
5 changes: 3 additions & 2 deletions pwnlib/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import os
import time

from six.moves.xmlrpc_client import ServerProxy

import packaging.version

from pwnlib.args import args
Expand Down Expand Up @@ -75,6 +73,9 @@ def available_on_pypi(prerelease=current_version.is_prerelease):
>>> available_on_pypi(prerelease=False).is_prerelease
False
"""
# Deferred import to save startup time
from six.moves.xmlrpc_client import ServerProxy

versions = getattr(available_on_pypi, 'cached', None)
if versions is None:
client = ServerProxy('https://pypi.python.org/pypi')
Expand Down