Skip to content

Instantly share code, notes, and snippets.

@Justsoos
Last active November 22, 2018 02:53
Show Gist options
  • Save Justsoos/4fb7d5af0ddf86d757f9efeb6bb570b3 to your computer and use it in GitHub Desktop.
Save Justsoos/4fb7d5af0ddf86d757f9efeb6bb570b3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# IMPORTANT! install the correct package: pip3 install html-table-parser-python3
# if puzzled by "from html_table_parser import HTMLTableParser, ImportError: cannot import name 'HTMLTableParser'"
# delete the dir of "python3/dist-packages/html_table_parser" manully and then reinstall, it comes from a bug of pip
import re
import requests
import argparse
# import logging
from requests.adapters import HTTPAdapter
from html_table_parser import HTMLTableParser
from prettytable import PrettyTable
sess = requests.Session()
sess.mount('', HTTPAdapter(max_retries=5))
headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Mobile Safari/537.36'}
# logging.getLogger().setLevel(logging.DEBUG)
# logging.debug('')
def ssip(ip):
ref_url = 'https://www.ipip.net/ip.html'
url = ref_url
sess.headers.update({'referer': ref_url})
for i in range(1,3):
html = sess.post(url, headers=headers, allow_redirects=False, data={'ip':ip})
if html.status_code==200:
break
p = HTMLTableParser()
p.feed(html.text)
rtb_html = re.search(r'"(https://ip\.rtbasia\.com[^"]+?)"', html.text)
if rtb_html:
for i in range(1,3):
iframe = sess.get(rtb_html.group(1), headers=headers)
if iframe.status_code==200:
break
rtb_text = re.search(r'>([^&>]+?)&nbsp;([^&]+?)&nbsp;([^<]+?)<', iframe.text)
if rtb_text.group(3):
rtb = ''.join(rtb_text.group(1,2,3))
else:
rtb = 'rtb error {}'.format(rtb_text.group(0))
else:
rtb = 'rtb html error'
p.tables[-1][1] = [rtb]
for y in p.tables:
t = PrettyTable(y.pop(0))
for val in y:
val = [s.replace('\r', '') for s in val]
t.add_row(val)
print(t)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('ip',nargs='?',default='')
args = parser.parse_args()
ip = args.ip
ssip(ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment