Last active
May 29, 2024 20:27
-
-
Save kaito834/af2ad953e3f47a6fde42 to your computer and use it in GitHub Desktop.
Python 3: urllib.request and json sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
urllib.request is defined separately in python3. If you already use python3, it should work just fine.
j = json.loads(res_body.decode("utf-8")) this line not working on my machine i use python 3 do you know why?
+1 I am also getting error in python 3
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
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
how do you install urllib.request? i can only find reading material not the download