Python re.I Examples

The following are 30 code examples for showing how to use re.I(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module re , or try the search function .

Example 1
Project: wafw00f   Author: EnableSecurity   File: main.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def matchHeader(self, headermatch, attack=False):
        if attack:
            r = self.attackres
        else: r = rq
        if r is None:
            return
        header, match = headermatch
        headerval = r.headers.get(header)
        if headerval:
            # set-cookie can have multiple headers, python gives it to us
            # concatinated with a comma
            if header == 'Set-Cookie':
                headervals = headerval.split(', ')
            else:
                headervals = [headerval]
            for headerval in headervals:
                if re.search(match, headerval, re.I):
                    return True
        return False 
Example 2
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 6 votes vote down vote up
def tweak(self, content):
        tweaked = self.legacy_tweak(content)
        if tweaked:
            return tweaked
        apply_persistence_to_all_lines = \
            0 < self.setup_params.persistence_size and \
            not self.config_is_persistence_aware(content)
        matching_re = r'^(\s*(%s)\s*)(.*)$' % self.BOOT_PARAMS_STARTER
        kernel_parameter_line_pattern = re.compile(
            matching_re,
            flags = re.I | re.MULTILINE)
        out = self.tweak_first_match(
            content,
            kernel_parameter_line_pattern,
            apply_persistence_to_all_lines,
            self.param_operations(),
            self.param_operations_for_persistence())
        
        return self.post_process(out) 
Example 3
Project: multibootusb   Author: mbusb   File: grub.py    License: GNU General Public License v2.0 6 votes vote down vote up
def tweak_bootfile_path(img_file_spec, isolinux_dir):
    """
    Function to find image files to boot and return them concatinated
    with a space. Return the spec untouched if no locations are found.
    :param kernel_file_spec: Image path specification lead by kernel/linux keyword within isolinux supported .cfg files.
    :param isolinux_dir: Path to isolinux directory of an ISO
    :return: Converted file paths joined by a space. If no files can be located, img_file_spec is returned unmodified.
    """
    kernel_line = ''

    raw_paths = img_file_spec.split(',')
    converted_paths = [locate_kernel_file(p, isolinux_dir) for p in raw_paths]
    if raw_paths != converted_paths: # Tweaked the paths successfully?
        return ' '.join(converted_paths)

    if 'z,' in img_file_spec:
        # Fallback to legacy code.
        # "... I found this only in dban"
        iso_dir = iso.isolinux_bin_dir(config.image_path)
        replacement = ' /multibootusb/' + iso.iso_basename(config.image_path) \
                      + '/' \
                      + iso_dir.replace('\\', '/') + '/'
        return img_file_spec.replace('z,', replacement)
    # Give up and return the original with replaced delimeters.
    return ' '.join(img_file_spec.split(',')) 
Example 4
Project: SublimeKSP   Author: nojanath   File: ksp_compiler.py    License: GNU General Public License v3.0 6 votes vote down vote up
def compact_names(self):
        global variables

        # build regular expression that can later tell which names to preserve (these should not undergo compaction)
        preserve_pattern = re.compile(r'[$%@!?~]?(' + '|'.join(self.variable_names_to_preserve) + ')$', re.I)

        for v in variables:
            if self.variable_names_to_preserve and preserve_pattern.match(v):
                #self.original2short[v] = v
                #self.short2original[v] = v
                continue
            elif v not in self.original2short and v not in ksp_builtins.variables:
                self.original2short[v] = '%s%s' % (v[0], compress_variable_name(v))
                if self.original2short[v] in ksp_builtins.variables:
                    raise Exception('This is your unlucky day. Even though the chance is only 3.2%%%% the variable %s was mapped to the same hash as that of a builtin KSP variable.' % (v))
                if self.original2short[v] in self.short2original:
                    raise Exception('This is your unlucky day. Even though the chance is only 3.2%%%% two variable names were compacted to the same short name: %s and %s' % (v, self.short2original[self.original2short[v]]))
                self.short2original[self.original2short[v]] = v
        ASTModifierIDSubstituter(self.original2short, force_lower_case=True).modify(self.module) 
Example 5
Project: cutout   Author: jojoin   File: cutout.py    License: MIT License 6 votes vote down vote up
def remove_html_tags(htmlstr):
    #先过滤CDATA
    re_cdata = re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #匹配CDATA
    re_script = re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
    re_style = re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
    re_br = re.compile('<br\s*?/?>')#处理换行
    re_h = re.compile('</?\w+[^>]*>')#HTML标签
    re_comment = re.compile('<!--[^>]*-->')#HTML注释
    s = re_cdata.sub('',htmlstr)#去掉CDATA
    s = re_script.sub('',s) #去掉SCRIPT
    s = re_style.sub('',s)#去掉style
    s = re_br.sub('\n',s)#将br转换为换行
    s = re_h.sub('',s) #去掉HTML 标签
    s = re_comment.sub('',s)#去掉HTML注释
    #去掉多余的空行
    blank_line = re.compile('\n+')
    s = blank_line.sub('\n',s)
    #s = recover_html_char_entity(s)#替换实体
    return s



## 替换常用HTML字符实体. 
Example 6
Project: jawfish   Author: war-and-code   File: utils.py    License: MIT License 6 votes vote down vote up
def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content)) 
Example 7
Project: Vxscan   Author: al0ne   File: ip_history.py    License: Apache License 2.0 6 votes vote down vote up
def ipinfo(host):
    out = []
    if not re.search(r'\d+\.\d+\.\d+\.\d+', host):
        req = Requests()
        # noinspection PyBroadException
        try:
            r = req.get('https://viewdns.info/iphistory/?domain={}'.format(host))
            result = re.findall(r'(?<=<tr><td>)\d+\.\d+\.\d+\.\d+(?=</td><td>)', r.text, re.S | re.I)
            if result:
                for i in result:
                    if iscdn(i):
                        out.append(i)
        except Exception:
            pass

    return out 
Example 8
Project: gist-alfred   Author: danielecook   File: utils.py    License: MIT License 6 votes vote down vote up
def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content)) 
Example 9
Project: hadrian   Author: modelop   File: tools.py    License: Apache License 2.0 6 votes vote down vote up
def getmatch(self, haystack):
        if not isinstance(haystack, basestring):
            return None
        flags = 0
        if self.flags is not None:
            if "i" in self.flags or "I" in self.flags:
                flags |= re.I
            if "l" in self.flags or "L" in self.flags:
                flags |= re.L
            if "m" in self.flags or "M" in self.flags:
                flags |= re.M
            if "s" in self.flags or "S" in self.flags:
                flags |= re.S
            if "u" in self.flags or "U" in self.flags:
                flags |= re.U
            if "x" in self.flags or "X" in self.flags:
                flags |= re.X
        if re.match(self.pattern, haystack, flags=flags) is None:
            return None
        elif self.to is None:
            return Match(haystack, haystack)
        else:
            return Match(haystack, re.sub(self.pattern, self.to, haystack, flags=flags)) 
Example 10
Project: recordlinkage   Author: J535D165   File: config.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _select_options(pat):
    """returns a list of keys matching `pat`

    if pat=="all", returns all registered options
    """

    # short-circuit for exact key
    if pat in _registered_options:
        return [pat]

    # else look through all of them
    keys = sorted(_registered_options.keys())
    if pat == 'all':  # reserved key
        return keys

    return [k for k in keys if re.search(pat, k, re.I)] 
Example 11
Project: pyspider   Author: binux   File: response.py    License: Apache License 2.0 6 votes vote down vote up
def get_encoding(headers, content):
    """Get encoding from request headers or page head."""
    encoding = None

    content_type = headers.get('content-type')
    if content_type:
        _, params = cgi.parse_header(content_type)
        if 'charset' in params:
            encoding = params['charset'].strip("'\"")

    if not encoding:
        content = utils.pretty_unicode(content[:1000]) if six.PY3 else content

        charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]',
                                flags=re.I)
        pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]',
                               flags=re.I)
        xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')
        encoding = (charset_re.findall(content) +
                    pragma_re.findall(content) +
                    xml_re.findall(content))
        encoding = encoding and encoding[0] or None

    return encoding 
Example 12
Project: misp42splunk   Author: remg427   File: utils.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content)) 
Example 13
Project: misp42splunk   Author: remg427   File: request.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, proxies=None, **x509):
        msg = "%(class)s style of invoking requests is deprecated. " \
              "Use newer urlopen functions/methods" % {'class': self.__class__.__name__}
        warnings.warn(msg, DeprecationWarning, stacklevel=3)
        if proxies is None:
            proxies = getproxies()
        assert hasattr(proxies, 'keys'), "proxies must be a mapping"
        self.proxies = proxies
        self.key_file = x509.get('key_file')
        self.cert_file = x509.get('cert_file')
        self.addheaders = [('User-Agent', self.version)]
        self.__tempfiles = []
        self.__unlink = os.unlink # See cleanup()
        self.tempcache = None
        # Undocumented feature: if you assign {} to tempcache,
        # it is used to cache files retrieved with
        # self.retrieve().  This is not enabled by default
        # since it does not work for changing documents (and I
        # haven't got the logic to check expiration headers
        # yet).
        self.ftpcache = ftpcache
        # Undocumented feature: you can use a different
        # ftp cache by assigning to the .ftpcache member;
        # in case you want logically independent URL openers
        # XXX This is not threadsafe.  Bah. 
Example 14
Project: misp42splunk   Author: remg427   File: utils.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content)) 
Example 15
Project: misp42splunk   Author: remg427   File: utils.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    warnings.warn((
        'In requests 3.0, get_encodings_from_content will be removed. For '
        'more information, please see the discussion on issue #2266. (This'
        ' warning should only appear once.)'),
        DeprecationWarning)

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content)) 
Example 16
Project: wafw00f   Author: EnableSecurity   File: main.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def matchContent(self, regex, attack=True):
        if attack:
            r = self.attackres
        else: r = rq
        if r is None:
            return
        # We may need to match multiline context in response body
        if re.search(regex, r.text, re.I):
            return True
        return False 
Example 17
Project: drydock   Author: airshipit   File: hostname_validity.py    License: Apache License 2.0 5 votes vote down vote up
def run_validation(self, site_design, orchestrator=None):
        # Check FQDN length is <= 255 characters per RFC 1035

        node_list = site_design.baremetal_nodes or []
        invalid_nodes = [
            n for n in node_list if len(n.get_fqdn(site_design)) > 255
        ]

        for n in invalid_nodes:
            msg = "FQDN %s is invalid, greater than 255 characters." % n.get_fqdn(
                site_design)
            self.report_error(
                msg, [n.doc_ref],
                "RFC 1035 requires full DNS names to be < 256 characters.")

        # Check each label in the domain name is <= 63 characters per RFC 1035
        # and only contains A-Z,a-z,0-9,-

        valid_label = re.compile('[a-z0-9-]{1,63}', flags=re.I)

        for n in node_list:
            domain_labels = n.get_fqdn(site_design).split('.')
            for domain_label in domain_labels:
                if not valid_label.fullmatch(domain_label):
                    msg = "FQDN %s is invalid - label '%s' is invalid." % (
                        n.get_fqdn(site_design), domain_label)
                    self.report_error(
                        msg, [n.doc_ref],
                        "RFC 1035 requires each label in a DNS name to be <= 63 characters and contain "
                        "only A-Z, a-z, 0-9, and hyphens.") 
Example 18
Project: drydock   Author: airshipit   File: middleware.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self):
        # Setup validation pattern for external marker
        UUIDv4_pattern = '^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'
        self.marker_re = re.compile(UUIDv4_pattern, re.I) 
Example 19
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def commentout_gfxboot(input_text):
    return re.sub(r'(ui\s+.*?gfxboot\.c32.*)$', r'# \1', input_text,
                  flags=re.I | re.MULTILINE) 
Example 20
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def extract_distroinfo_from_file(self, subpath, regex, distro_group,
                                    version_group):
        content = self.file_content(subpath)
        if not content:
            return None
        m = re.compile(regex, re.I).search(content)
        if not m:
            return None
        return (m.group(distro_group),
                [int(x) for x in m.group(version_group).split('.')]) 
Example 21
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def extract_distroinfo_from_fname(self, which_dir, regex, distro_group,
                                      version_group):
        p = re.compile(regex, re.I)
        for fname in os.listdir(self.fullpath(which_dir)):
            m = p.match(fname)
            if m:
                return (m.group(distro_group),
                        [int(x) for x in m.group(version_group).split('.')])
        return None 
Example 22
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kw):
        persistence_awareness_checking_re = re.compile(
            r'^\s*(%s).*?\s(rd.live.overlay|overlay)=.+?' %
            self.BOOT_PARAMS_STARTER,  flags=re.I|re.MULTILINE)
        super(FedoraConfigTweaker, self).__init__(
            persistence_awareness_checking_re, *args, **kw) 
Example 23
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def post_process(self, s):
        s = re.sub(r'^(\s*UI\s+(.*?gfxboot(\.c32|)))\s+(.*?)\s+(.*)$',
                   r'# \1 \4.renamed-to-avoid-lockup \5', s,
                   flags=re.I + re.MULTILINE)
        return s 
Example 24
Project: multibootusb   Author: mbusb   File: gen.py    License: GNU General Public License v2.0 5 votes vote down vote up
def grub_efi_exist(grub_efi_path):
    """
    Detect efi present in USB disk is copied by multibootusb.
    :param isolinux_path: Path to "grub efi image"
    :return: True if yes else False
    """
    from . import iso
    if grub_efi_path is not None:
        sl = list(iso.strings(grub_efi_path))
        for strin in sl:
            if re.search(r'multibootusb', strin, re.I):
                return True
        return False 
Example 25
Project: multibootusb   Author: mbusb   File: iso.py    License: GNU General Public License v2.0 5 votes vote down vote up
def isolinux_version(isolinux_bin_path):
    """
    Detect isolinux version shipped by distros.
    :param isolinux_path: Path to "isolinux.bin"
    :return: Version number as string.
    """
    version = ["2", "3", "4", "5", "6"]
    if isolinux_bin_path is not None:
        sl = list(strings(isolinux_bin_path))
        for strin in sl:
            if re.search(r'isolinux ', strin, re.I):
                for number in version:
                    if re.search(r'isolinux ' + number, strin, re.I):
                        log("\nFound syslinux version " + number + "\n")
                        return str(number) 
Example 26
Project: DOTA_models   Author: ringringyi   File: create_pet_tf_record.py    License: Apache License 2.0 5 votes vote down vote up
def get_class_name_from_filename(file_name):
  """Gets the class name from a file.

  Args:
    file_name: The file name to get the class name from.
               ie. "american_pit_bull_terrier_105.jpg"

  Returns:
    example: The converted tf.Example.
  """
  match = re.match(r'([A-Za-z_]+)(_[0-9]+\.jpg)', file_name, re.I)
  return match.groups()[0] 
Example 27
Project: object_detector_app   Author: datitran   File: create_pet_tf_record.py    License: MIT License 5 votes vote down vote up
def get_class_name_from_filename(file_name):
  """Gets the class name from a file.

  Args:
    file_name: The file name to get the class name from.
               ie. "american_pit_bull_terrier_105.jpg"

  Returns:
    example: The converted tf.Example.
  """
  match = re.match(r'([A-Za-z_]+)(_[0-9]+\.jpg)', file_name, re.I)
  return match.groups()[0] 
Example 28
Project: oscrypto   Author: wbond   File: test_tls.py    License: MIT License 5 votes vote down vote up
def tls_connect(self, hostname, port):
        session = None
        if hostname == 'dh1024.badtls.io':
            session = tls.TLSSession(extra_trust_roots=[badtls_ca_path])
        connection = tls.TLSSocket(hostname, port, session=session)
        self.assertEqual(hostname, connection.hostname)
        self.assertIsInstance(connection.hostname, str_cls)
        self.assertIsInstance(connection.cipher_suite, str_cls)
        self.assertIsInstance(connection.certificate, x509.Certificate)
        self.assertLess(10, len(connection.cipher_suite))
        self.assertEqual(port, connection.port)
        connection.write(b'GET / HTTP/1.1\r\nHost: ' + hostname.encode('utf-8') + b'\r\n\r\n')
        html = connection.read_until(re.compile(b'</html>', re.I))
        self.assertNotEqual(None, re.search(b'</html>', html, re.I)) 
Example 29
Project: oscrypto   Author: wbond   File: test_tls.py    License: MIT License 5 votes vote down vote up
def test_tls_error_weak_dh_params(self):
        # badssl.com uses SNI, which Windows XP does not support
        regex = 'weak DH parameters' if not xp else 'self-signed'
        # ideally we would use badtls.io since that does not require SNI, however
        # it is not possible to force a good version of OpenSSL to use such a
        # small value for DH params, and I don't feel like the headache of trying
        # to get an old, staticly-linked socat set up just for this text on XP
        with assert_exception(self, errors.TLSError, regex):
            tls.TLSSocket('dh512.badssl.com', 443) 
Example 30
Project: oscrypto   Author: wbond   File: _tls.py    License: MIT License 5 votes vote down vote up
def detect_other_protocol(server_handshake_bytes):
    """
    Looks at the server handshake bytes to try and detect a different protocol

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        None, or a unicode string of "ftp", "http", "imap", "pop3", "smtp"
    """

    if server_handshake_bytes[0:5] == b'HTTP/':
        return 'HTTP'

    if server_handshake_bytes[0:4] == b'220 ':
        if re.match(b'^[^\r\n]*ftp', server_handshake_bytes, re.I):
            return 'FTP'
        else:
            return 'SMTP'

    if server_handshake_bytes[0:4] == b'220-':
        return 'FTP'

    if server_handshake_bytes[0:4] == b'+OK ':
        return 'POP3'

    if server_handshake_bytes[0:4] == b'* OK' or server_handshake_bytes[0:9] == b'* PREAUTH':
        return 'IMAP'

    return None