mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-12-18 00:00:14 +00:00
Merge pull request #93 from Hafpaf/master
add extended_ip_address_info.py
This commit is contained in:
commit
760ba8492c
|
@ -28,6 +28,7 @@ So far, the following projects have been integrated to this repo:
|
||||||
|[Directory organizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Directory-organizer) | [Athul P](https://github.com/athulpn) |
|
|[Directory organizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Directory-organizer) | [Athul P](https://github.com/athulpn) |
|
||||||
|[Excel Files Merger](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Excel_Files_Merger) | [Andrei N](https://github.com/Andrei-Niculae)|
|
|[Excel Files Merger](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Excel_Files_Merger) | [Andrei N](https://github.com/Andrei-Niculae)|
|
||||||
|[Excel to List](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Excel_to_ListofList) | [Nitish Srivastava](https://github.com/nitish-iiitd)|
|
|[Excel to List](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Excel_to_ListofList) | [Nitish Srivastava](https://github.com/nitish-iiitd)|
|
||||||
|
|[Extended_ip_address_info](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/extended_ip_address_info) | [hafpaf](https://github.com/hafpaf)|
|
||||||
|[File explorer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/File-Explorer-Dialog-Box) | [Nikhil Kumar Singh](https://github.com/nikhilkumarsingh)|
|
|[File explorer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/File-Explorer-Dialog-Box) | [Nikhil Kumar Singh](https://github.com/nikhilkumarsingh)|
|
||||||
|[File Sharing Bot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/File-Sharing-Bot) | [Darshan Patel](https://github.com/DarshanPatel11)|
|
|[File Sharing Bot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/File-Sharing-Bot) | [Darshan Patel](https://github.com/DarshanPatel11)|
|
||||||
|[Flash card quizzer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Flash-card-Challenge) |[Utkarsh Sharma](https://github.com/Utkarsh1308) |
|
|[Flash card quizzer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Flash-card-Challenge) |[Utkarsh Sharma](https://github.com/Utkarsh1308) |
|
||||||
|
|
52
extended_ip_address_info/README.md
Normal file
52
extended_ip_address_info/README.md
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
# Extended IP address info
|
||||||
|
|
||||||
|
View extended info about your public IP address from the terminal.
|
||||||
|
|
||||||
|
The python script runs `curl` with the following parameters
|
||||||
|
```bash
|
||||||
|
curl -H "Accept: application/json" https://ipinfo.io/json
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create virtual environment and run
|
||||||
|
Create virtual environment.
|
||||||
|
```bash
|
||||||
|
python3 -m venv /path/to/new/virtual/environment
|
||||||
|
```
|
||||||
|
|
||||||
|
Activate virtual environment.
|
||||||
|
```bash
|
||||||
|
cd <file folder>
|
||||||
|
source bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
Install required libraries.
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
**Run program.**
|
||||||
|
```bash
|
||||||
|
python extended_ip_address_info.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Deactivate virtual environment.
|
||||||
|
```bash
|
||||||
|
deactivate
|
||||||
|
```
|
||||||
|
|
||||||
|
## Output
|
||||||
|
Output should be in the form of the following:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"ip": "xxx.xxx.xxx.xxx",
|
||||||
|
"city": "A_city",
|
||||||
|
"hostname": "host.isp-website.com",
|
||||||
|
"region": "A_region",
|
||||||
|
"country": "Country code",
|
||||||
|
"loc": "coordinates",
|
||||||
|
"org": "AS-number ISP-name",
|
||||||
|
"postal": "postal-code",
|
||||||
|
"timezone": "Europe/City",
|
||||||
|
"readme": "https://ipinfo.io/missingauth"
|
||||||
|
}
|
||||||
|
```
|
28
extended_ip_address_info/extended_ip_address_info.py
Normal file
28
extended_ip_address_info/extended_ip_address_info.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#!/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'))
|
2
extended_ip_address_info/requirements.txt
Normal file
2
extended_ip_address_info/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
pycurl
|
||||||
|
certifi
|
Loading…
Reference in New Issue
Block a user