|
19 | 19 | import socket |
20 | 20 | import ipaddress |
21 | 21 | import os |
| 22 | +import json |
| 23 | +import re |
22 | 24 |
|
23 | 25 | if sys.version < '3': |
| 26 | + import urllib, httplib |
| 27 | + def urlencode(x): |
| 28 | + return urllib.urlencode(x) |
| 29 | + def httprequest(x, usessl): |
| 30 | + try: |
| 31 | + # conn = httplib.HTTPConnection("api.ip2proxy.com") |
| 32 | + if (usessl is True): |
| 33 | + conn = httplib.HTTPSConnection("api.ip2proxy.com") |
| 34 | + else: |
| 35 | + conn = httplib.HTTPConnection("api.ip2proxy.com") |
| 36 | + conn.request("GET", "/?" + x) |
| 37 | + res = conn.getresponse() |
| 38 | + return json.loads(res.read()) |
| 39 | + except: |
| 40 | + return None |
24 | 41 | def u(x): |
25 | 42 | return x.decode('utf-8') |
26 | 43 | def b(x): |
27 | 44 | return str(x) |
28 | 45 | else: |
| 46 | + import urllib.parse, http.client |
| 47 | + def urlencode(x): |
| 48 | + return urllib.parse.urlencode(x) |
| 49 | + def httprequest(x, usessl): |
| 50 | + try: |
| 51 | + # conn = http.client.HTTPConnection("api.ip2proxy.com") |
| 52 | + if (usessl is True): |
| 53 | + conn = http.client.HTTPSConnection("api.ip2proxy.com") |
| 54 | + else: |
| 55 | + conn = http.client.HTTPConnection("api.ip2proxy.com") |
| 56 | + conn.request("GET", "/?" + x) |
| 57 | + res = conn.getresponse() |
| 58 | + return json.loads(res.read()) |
| 59 | + except: |
| 60 | + return None |
29 | 61 | def u(x): |
30 | 62 | if isinstance(x, bytes): |
31 | 63 | return x.decode() |
@@ -53,7 +85,7 @@ def inet_pton(t, addr): |
53 | 85 | return out_addr_p.raw |
54 | 86 | socket.inet_pton = inet_pton |
55 | 87 |
|
56 | | -_VERSION = '3.2.1' |
| 88 | +_VERSION = '3.3.0' |
57 | 89 | _NO_IP = 'MISSING IP ADDRESS' |
58 | 90 | _FIELD_NOT_SUPPORTED = 'NOT SUPPORTED' |
59 | 91 | _INVALID_IP_ADDRESS = 'INVALID IP ADDRESS' |
@@ -623,3 +655,37 @@ def _get_record(self, ip): |
623 | 655 | high = mid - 1 |
624 | 656 | else: |
625 | 657 | low = mid + 1 |
| 658 | + |
| 659 | +class IP2ProxyWebService(object): |
| 660 | + ''' IP2Proxy web service ''' |
| 661 | + def __init__(self,apikey,package,usessl=True): |
| 662 | + if ((re.match(r"^[0-9A-Z]{10}$", apikey) == None) and (apikey != 'demo')): |
| 663 | + raise ValueError("Please provide a valid IP2Proxy web service API key.") |
| 664 | + if (re.match(r"^PX[0-9]+$", package) == None): |
| 665 | + package = 'PX1' |
| 666 | + self.apikey = apikey |
| 667 | + self.package = package |
| 668 | + self.usessl = usessl |
| 669 | + |
| 670 | + def lookup(self,ip): |
| 671 | + '''This function will look the given IP address up in IP2Proxy web service.''' |
| 672 | + parameters = urlencode((("ip", ip), ("key", self.apikey), ("package", self.package))) |
| 673 | + response = httprequest(parameters, self.usessl) |
| 674 | + if (response == None): |
| 675 | + return False |
| 676 | + if (('response' in response) and (response['response'] != "OK")): |
| 677 | + raise IP2ProxyAPIError(response['response']) |
| 678 | + return response |
| 679 | + |
| 680 | + def getcredit(self): |
| 681 | + '''Get the remaing credit in your IP2Proxy web service account.''' |
| 682 | + parameters = urlencode((("key", self.apikey), ("check", True))) |
| 683 | + response = httprequest(parameters, self.usessl) |
| 684 | + if (response == None): |
| 685 | + return 0 |
| 686 | + if ('response' in response is False): |
| 687 | + return 0 |
| 688 | + return response['response'] |
| 689 | + |
| 690 | +class IP2ProxyAPIError(Exception): |
| 691 | + """Raise for IP2Proxy API Error Message""" |
0 commit comments