-1

i have proxy string:

proxy = '127.0.0.1:8080'

i need check is it real string:

def is_proxy(proxy):
    return not any(c.isalpha() for c in proxy)

to skip string like:

fail_proxy = 'This is proxy: 127.0.0.1:8080'

but some time i have like:

fail_proxy2 = '127.0.0.1:8080\r'
is_proxy(fail_proxy2) is True
True

need False

1 Answers1

0

Try the following specific approach using re module(regexp):

import re

def is_proxy(proxy):
    return re.fullmatch('^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}:\d{1,5}$', proxy) is not None

proxy1 = '127.0.0.1:8080'
proxy2 = '127.0.0.1:8080\r'

print(is_proxy(proxy1))   # True
print(is_proxy(proxy2))   # False

As for port number (\d{1,5}): range 1-65535 are available for port numbers

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105