python http get json code example
Example 1: how to send get request python
import requests
requests.get("https://www.google.com/")
Example 2: how to read a json resposnse from a link in python
import urllib, json
url = "put url here"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
print (data)
Example 3: python http request post json example
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
Example 4: fetch a json from url python
import requests
r = requests.get('url')
print r.json()
Example 5: python requests get
# pip install requests
import requests
req = requests.get('<url here>', 'html.parser')
print(req.text)
Example 6: send get request python
import socket
target_host = "www.google.com"
target_port = 80
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
client.connect((target_host,target_port))
# receive some data
response = client.recv(4096)
print(f'Source Code: {response}')
http_response = repr(response)