Skip to content

Instantly share code, notes, and snippets.

@reikoNeko
Last active September 25, 2017 16:33
Show Gist options
  • Save reikoNeko/acd3732fb9f6b24bc1d27a805171ebad to your computer and use it in GitHub Desktop.
Save reikoNeko/acd3732fb9f6b24bc1d27a805171ebad to your computer and use it in GitHub Desktop.
A simple TCP listener that echoes what you send it. Works in Python 3 and 2. Based on the python2 listener in Black Hat Python.
#!/usr/env/python
from __future__ import print_function
import socket
import threading
bind_ip = "127.0.0.1"
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print("[*] Listening on %s:%d" % (bind_ip,bind_port))
# this is our client-handling thread
def handle_client(client_socket):
# print out what the client sends
request = client_socket.recv(1024)
print("[*] Received: %s" % request)
# send back a packet
client_socket.send(b"ACK!\n"+request)
client_socket.close()
while True:
client,addr = server.accept()
print("[*] Accepted connection from: %s:%d" % (addr[0],addr[1]))
# spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment