Skip to content

Instantly share code, notes, and snippets.

@harendra21
Created May 12, 2022 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harendra21/7e2b9d731e391cd97065931c4038c1a8 to your computer and use it in GitHub Desktop.
Save harendra21/7e2b9d731e391cd97065931c4038c1a8 to your computer and use it in GitHub Desktop.
#!/bin/python
# -*- coding: utf-8 -*-
# Using curl to get data from https://ipinfo.io/json
# Template from pycurl documentation
# http://pycurl.io/docs/latest/quickstart.html#examining-response-headers
import pycurl #curl library
import certifi #HTTP over TLS/SSL library
from io import BytesIO #Buffered I/O implementation using an in-memory bytes buffer.
#set header, '--header' or -H
header = ['Accept: application/json']
buffer = BytesIO()
c = pycurl.Curl() #curl
c.setopt(c.HTTPHEADER, header) #header
c.setopt(c.URL, 'https://ipinfo.io/json') #URL
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.CAINFO, certifi.where()) # SSL certificates
c.perform()
c.close()
body = buffer.getvalue()
# Body is a byte string.
# We have to know the encoding in order to print it to a text file
# such as standard output.
print(body.decode('iso-8859-1'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment