Skip to content

Instantly share code, notes, and snippets.

@kaito834
Last active July 19, 2023 23:37
Show Gist options
  • Save kaito834/af2ad953e3f47a6fde42 to your computer and use it in GitHub Desktop.
Save kaito834/af2ad953e3f47a6fde42 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.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
import urllib.request
import json
# https://docs.python.org/3/library/urllib.request.html#examples
# https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html
res = urllib.request.urlopen('https://ip-ranges.amazonaws.com/ip-ranges.json')
res_body = res.read()
# https://docs.python.org/3/library/json.html
j = json.loads(res_body.decode("utf-8"))
# 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)
@Hames0024
Copy link

This code is not working to github.com
Do you know?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment