Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 23: IP address fetch and display #90

Merged
merged 7 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asyncio, websockets, sys, click, time, os
import asyncio, websockets, sys, click, http.client, time
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's as optimized as it gets. Good work!

from prompt_toolkit import print_formatted_text, HTML
from websockets.exceptions import ConnectionClosedError
from utils.helper_display import HelperDisplay
Expand All @@ -20,6 +20,21 @@ def obtntime():
return timehour + ":" + timemint + ":" + timesecs


def ipaddress(v):
url = "api64.ipify.org"
if v == 4:
url = "api.ipify.org"
elif v == 6:
url = "api6.ipify.org"
try:
connection = http.client.HTTPSConnection(url)
connection.request("GET", "/")
response = connection.getresponse()
return response.read().decode("UTF-8")
except:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generalized exception handling would prevent service crashes in all times.

return "Error getting IP address."


def getallus(chatroom):
userlist = []
for indx in USERS:
Expand Down Expand Up @@ -104,9 +119,11 @@ def mainfunc(chatport, netprotc):
if netprotc == "ipprotv6":
print_formatted_text(HTML("[" + obtntime() + "] " + "<b>SNCTRYZERO</b> > <green>IP version : 6</green>"))
netpdata = "::"
print_formatted_text(HTML("[" + obtntime() + "] " + "<b>SNCTRYZERO</b> > <green>IP address : " + ipaddress(6) + "</green>"))
elif netprotc == "ipprotv4":
print_formatted_text(HTML("[" + obtntime() + "] " + "<b>SNCTRYZERO</b> > <green>IP version : 4</green>"))
netpdata = "0.0.0.0"
print_formatted_text(HTML("[" + obtntime() + "] " + "<b>SNCTRYZERO</b> > <green>IP address : " + ipaddress(4) + "</green>"))
servenow(netpdata, chatport)
except OSError:
print_formatted_text(HTML("[" + obtntime() + "] " + "<b>SNCTRYZERO</b> > <red><b>The server could not be started up</b></red>"))
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ prompt-toolkit==3.0.8
pycparser==2.20
six==1.15.0
wcwidth==0.2.5
websockets==8.1
websockets==8.1