File: http.py

package info (click to toggle)
weevely 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,336 kB
  • sloc: python: 7,732; php: 1,035; sh: 53; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from core.weexceptions import FatalException
from core import messages
from core import config
import random
import string
import utils
import urllib.request, urllib.error, urllib.parse
import os

agents_list_path = 'utils/_http/user-agents.txt'

def load_all_agents():

    try:

        with open(
            os.path.join(config.weevely_path,
            agents_list_path)
            ) as agents_file:
            return agents_file.read().split('\n')

    except Exception as e:
        raise FatalException(
            messages.generic.error_loading_file_s_s %
            (agents_list_path, str(e)))


def add_random_url_param(url):

    random_param = '%s=%s' % (
        utils.strings.randstr(
            n = 4,
            fixed = False,
            charset = string.ascii_letters
        ),
        utils.strings.randstr(
            n = 10,
            fixed = False
        )
    )

    if '?' not in url:
        url += '?%s' % random_param
    else:
        url += '&%s' % random_param

    return url

def request(url, headers = []):
    
    if not next((x for x in headers if x[0] == 'User-Agent'), False):
        headers = [ ('User-Agent', random.choice(load_all_agents())) ]

    opener = urllib.request.build_opener()
    opener.addheaders = headers
    return opener.open(url).read()