Extracting IP addresses from a file
<p>I'm trying to extract IP addresses from an <code>asp</code> file in Python, the file looks something like this:</p>

<pre><code>onInternalNet = (
        isInNet(hostDNS, "147.163.1.0", "255.255.0.0") ||
        isInNet(hostDNS, "123.264.0.0", "255.255.0.0") ||
        isInNet(hostDNS, "137.5.0.0", "255.0.0.0") ||
        isInNet(hostDNS, "100.01.02.0", "255.0.0.0") ||
        isInNet(hostDNS, "172.146.30.0", "255.240.0.0") ||
        isInNet(hostDNS, "112.268.0.0", "255.255.0.0") ||
</code></pre>

<p>How I'm attempting to extract them is with a regex:</p>

<pre><code>if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
</code></pre>

<p>However I'm getting an error:</p>

<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File "pull_proxy.py", line 27, in &lt;module&gt;
    write_to_file(extract_proxies(in_file), out_file)
  File "pull_proxy.py", line 8, in extract_proxies
    if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
  File "C:\Python27\lib\re.py", line 194, in compile
    return _compile(pattern, flags)
  File "C:\Python27\lib\re.py", line 233, in _compile
    bypass_cache = flags &amp; DEBUG
TypeError: unsupported operand type(s) for &amp;: 'str' and 'int'
</code></pre>

<p>I don't understand why I'm getting that error, what can I do to this code to make it extract the information like I want it to?</p>

<pre><code>import re

def extract_proxies(in_file):
    buffer = []

    for line in in_file:
        if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
            print "{} appened to buffer.".format(line)
            buffer.append(line)
        else:
            pass

    return buffer

def write_to_file(buffer, out_file):
    for proxy in buffer:
        with open(out_file, "a+") as res:
            res.write(proxy)

if __name__ == '__main__':
    print "Running...."
    in_file = "C:/Users/thomas_j_perkins/Downloads/test.asp"
    out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
    write_to_file(extract_proxies(in_file), out_file)
</code></pre>

<p><strong>EDIT</strong></p>

<p>Realized I hadn't opened the file:</p>

<pre><code>import re

def extract_proxies(in_file):
    buffer = []

    for line in in_file:
        if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
            print "{} appened to buffer.".format(line)
            buffer.append(line)
        else:
            pass

    in_file.close()
    return buffer

def write_to_file(buffer, out_file):
    for proxy in buffer:
        with open(out_file, "a+") as res:
            res.write(proxy)

if __name__ == '__main__':
    print "Running...."
    in_file = "C:/Users/thomas_j_perkins/Downloads/PAC-Global-Vista.asp"
    out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
    write_to_file(extract_proxies(open(in_file, "r+")), out_file)
</code></pre>

<p>Still getting the same error:</p>

<pre class="lang-none prettyprint-override"><code>Running....
Traceback (most recent call last):
  File "pull_proxy.py", line 28, in &lt;module&gt;
    write_to_file(extract_proxies(open(in_file)), out_file)
  File "pull_proxy.py", line 8, in extract_proxies
    if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
  File "C:\Python27\lib\re.py", line 194, in compile
    return _compile(pattern, flags)
  File "C:\Python27\lib\re.py", line 233, in _compile
    bypass_cache = flags &amp; DEBUG
TypeError: unsupported operand type(s) for &amp;: 'str' and 'int'
</code></pre>

<p>Please check the below code:</p>

<p>Did couple of changes</p>

<ol>
<li>re.compile - Regex should be complied first and then can be used with 'match/search/findall'.</li>
<li>Regex was not proper. While writing regex we need to consider from the start of line. Regex didn't match words in between line directly.</li>
</ol>

<p></p>

<pre><code> import re


    def extract_proxies(in_file):
        buffer1 = []
        #Regex compiled here
        m = re.compile(r'\s*\w+\(\w+,\s+\"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\"')

        for line in in_file:
            #Used here to match
            r = m.match(line)
            if r is not None:
                print "{} appened to buffer.".format(line)
                buffer1.append(r.group(1))
            else:
                pass

        in_file.close()
        return buffer1


    def write_to_file(buffer1, out_file):
        for proxy in buffer1:
            with open(out_file, "a+") as res:
                res.write(proxy+'\n')


    if __name__ == '__main__':
        print "Running...."
        in_file = "sample.txt"
        out_file = "results.txt"
        write_to_file(extract_proxies(open(in_file)), out_file)
</code></pre>

<p>Output:</p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Running....
        isInNet(hostDNS, "147.163.1.0", "255.255.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "123.264.0.0", "255.255.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "137.5.0.0", "255.0.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "100.01.02.0", "255.0.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "172.146.30.0", "255.240.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "112.268.0.0", "255.255.0.0") || appened to buffer.

C:\Users\dinesh_pundkar\Desktop&gt;python c.py
</code></pre>

python python-2.7 extract ipv4