Skip to content

Instantly share code, notes, and snippets.

@kaito834
Last active July 19, 2023 23:37
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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)
@ophiuchus44
Copy link

how do you install urllib.request? i can only find reading material not the download

@azizimusa
Copy link

urllib.request is defined separately in python3. If you already use python3, it should work just fine.

@kemalekren
Copy link

j = json.loads(res_body.decode("utf-8")) this line not working on my machine i use python 3 do you know why?

@Murthysagi
Copy link

+1 I am also getting error in python 3

@acmaheri
Copy link

acmaheri commented Jun 6, 2019

urllib.request is defined separately in python3. If you already use python3, it should work just fine.

make sure you are using Python version 3 and then pip install urllib3

@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