Skip to content

Instantly share code, notes, and snippets.

@tuhin47
Last active August 30, 2023 18:24
Show Gist options
  • Save tuhin47/d984b3457ed4306e9ea732ac6e3fcf24 to your computer and use it in GitHub Desktop.
Save tuhin47/d984b3457ed4306e9ea732ac6e3fcf24 to your computer and use it in GitHub Desktop.
Find Dynamic Public IP in my router

FindMyIP.py

Preview:
import socket
import concurrent.futures

def ping_ip_port(ip, port):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(2)
        s.connect((ip, port))
        print("http://{}:{}".format(ip,port))
        return True
    except Exception:
        return False

# TODO Update port and ip pattern
port_number = 8080
ip_format="127.0.0.{0}"

executor = concurrent.futures.ThreadPoolExecutor(max_workers=255)
futures = []

for ip_int in range(0, 255 + 1):
    ip=ip_format.format(ip_int)
    future = executor.submit(ping_ip_port, ip, port_number)
    futures.append(future)

for future in concurrent.futures.as_completed(futures):
    future.result()
Associated Context
Type Code Snippet ( .py )
Associated Tags concurrent library Concurrent programming nonblocking Exception handling Asynchronous programming Socket module ThreadPoolExecutor Socket programming Framework: concurrent.futures Port number change twisted IP address formatting Port scanning ThreadPoolExecutor class IP address scanning python-multithreading Error handling Data processing blocking python-asyncio Network communication Future management IP address calculation sockets Socket timeout Network connectivity
📝 Custom Description Find Dynamic Public IP in my router
💡 Smart Description This code connects to a specified IP address and port, creates a ThreadPoolExecutor for each ip in range 8080.
The code snippet uses concurrent.futures and socket to check if a given IP address and port number are reachable. It creates a thread pool to concurrently check multiple IP addresses and prints the reachable ones.
🔎 Suggested Searches How to create a ping IP port using concurrent
How to use ThreadPoolExecutor for TCP connections?
Programming with futures and FutureCompletionService in Python
Using ThreadPoolExecutor on UDP communications
Python code to ping IP address and port
Python socket programming code for checking connectivity
Python code for scanning IP addresses and ports
Python code to check if a port is open on an IP address
Python code for concurrent port scanning using threads
Related Links https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
https://gist.github.com/tuhin47/fe95fa4f51c10d50e3d10ab51538eccd
https://realpython.com/python-sockets/
https://docs.python.org/3/library/concurrent.futures.html
Related People No Related People
Sensitive Information No Sensitive Information Detected
Shareable Link https://tuhin47.pieces.cloud/?p=676e499417
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment