Skip to content

Instantly share code, notes, and snippets.

@raresteak
Last active July 15, 2022 17:36
Show Gist options
  • Save raresteak/ab9184f6e7c98ad7de5d6508672a807c to your computer and use it in GitHub Desktop.
Save raresteak/ab9184f6e7c98ad7de5d6508672a807c to your computer and use it in GitHub Desktop.
TCP Port listener using Python
#!/usr/bin/env python3
import socket
import datetime
import time
HOST = '0.0.0.0'
PORT = 8080
def listener():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
dateTime = datetime.datetime.now()
print('Connected by', addr , '@' , str(dateTime))
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
while True:
print("Starting listener on: " , HOST ,':', str(PORT))
try:
listener()
except:
pass
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment