Skip to content

Instantly share code, notes, and snippets.

@Murthysagi
Forked from kaito834/urllib-request-Request.py
Last active February 12, 2019 07:24
Show Gist options
  • Save Murthysagi/de6776e6fc7dfb396263bc197d3f8697 to your computer and use it in GitHub Desktop.
Save Murthysagi/de6776e6fc7dfb396263bc197d3f8697 to your computer and use it in GitHub Desktop.
Python 3: urllib.request and json sample
#!/usr/bin/env python
#
# tested by Python 3.4.3 on Windows 8.1
# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
# If you need to access web site/service via proxy, set http_proxy or https_proxy.
# https://docs.python.org/3/library/urllib.request.html#urllib.request.ProxyHandler
# set http_proxy=http://127.0.0.1:8888/
# set https_proxy=https://127.0.0.1:8888/
import urllib.request
# https://docs.python.org/3/library/urllib.request.html#examples
# "Here is an example of doing a PUT request using Request:"
header={'CustomHeader': 'CustomValue'}
req = urllib.request.Request(url='http://127.0.0.1:8080/', headers=header, method='DELETE')
res = urllib.request.urlopen(req, timeout=5)
print(res.status)
print(res.reason)
exit(0)
C:\nmap-6.46>ncat -v
Ncat: Version 6.46 ( http://nmap.org/ncat )
Ncat: You must specify a host to connect to. QUITTING.
C:\nmap-6.46>ncat -l -p 8080
DELETE / HTTP/1.1
Accept-Encoding: identity
User-Agent: Python-urllib/3.4
Connection: close
Customheader: CustomValue
Host: 127.0.0.1:8080
#!/usr/bin/env python
#
# tested by Python 3.6 on Windows 10
# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 12 2019, 22:43:06) on win64
import urllib.request
import json
url= 'https://ip-ranges.amazonaws.com/ip-ranges.json'
# https://docs.python.org/3/library/urllib.request.html#examples
# https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html
r = urllib.request.urlopen(url)
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))
print(data)
# parse strings: 'ip_prefix' and 'region'
#for i in range(len(j['prefixes'])):
# print("{0}\t{1}".format(j['prefixes'][i]['ip_prefix'], j['prefixes'][i]['region']))
for prefix in j['prefixes']:
print("{0}\t{1}".format(prefix['ip_prefix'], prefix['region']))
exit(0)
23.20.0.0/14 us-east-1
27.0.0.0/22 ap-northeast-1
43.250.192.0/24 ap-southeast-1
43.250.193.0/24 ap-southeast-1
46.51.128.0/18 eu-west-1
(snip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment