Skip to content

Commit

Permalink
Issue #78: Random user agent by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Jul 15, 2020
1 parent 8538670 commit c0a9d8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dirhunt/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import random

USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 '
'Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 '
'Safari/537.36 Edge/17.17134',
'Mozilla/5.0 (Linux; U; Android 2.2) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
]


def get_random_user_agent():
return random.choice(USER_AGENTS)
4 changes: 4 additions & 0 deletions dirhunt/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from proxy_db.proxies import ProxiesList
from proxy_db.models import Proxy

from dirhunt.agents import get_random_user_agent

if sys.version_info < (3, 0):
ConnectionError = IOError
Expand Down Expand Up @@ -82,6 +83,9 @@ def __init__(self, sessions, proxy):
self.proxy_name = proxy
self.proxy = normalize_proxy(self.proxy_name, sessions)
self.session = requests.Session()
self.session.headers = {
'User-Agent': get_random_user_agent(),
}
adapter = HTTPAdapter(pool_connections=POOL_CONNECTIONS, pool_maxsize=POOL_CONNECTIONS)
self.session.mount('http://', adapter)
self.session.mount('https://', adapter)
Expand Down

0 comments on commit c0a9d8b

Please sign in to comment.