Spaces:
Build error
Build error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
user_agents_url = 'https://raw.githubusercontent.com/danielmiessler/SecLists/master/Fuzzing/User-Agents/UserAgents-IE.txt'
|
| 6 |
+
|
| 7 |
+
os.makedirs('./tmp', exist_ok=True)
|
| 8 |
+
download_path = './tmp/user_agents.txt'
|
| 9 |
+
|
| 10 |
+
# download user-agents
|
| 11 |
+
if not os.path.exists(download_path):
|
| 12 |
+
urlretrieve(user_agents_url, download_path)
|
| 13 |
+
|
| 14 |
+
# reading user-agents file
|
| 15 |
+
with open(download_path, 'r') as f:
|
| 16 |
+
user_agents = f.readlines()
|
| 17 |
+
user_agents = list(map(lambda x: x.strip('\n'), user_agents))
|
| 18 |
+
|
| 19 |
+
def return_user_agent():
|
| 20 |
+
"""
|
| 21 |
+
this function returns different user agent randomly
|
| 22 |
+
"""
|
| 23 |
+
ua = random.choice(user_agents)
|
| 24 |
+
# using this header to pretend as regular user so that we are not blocked by website
|
| 25 |
+
headers = {
|
| 26 |
+
'User-Agent': ua
|
| 27 |
+
}
|
| 28 |
+
return headers
|