Skip to content

Instantly share code, notes, and snippets.

@craig552uk
Created Sep 12, 2014
Embed
What would you like to do?
A netcat type thing in python
#
# > telnet localhost 4000
# Trying 127.0.0.1...
# Connected to localhost.
# Escape character is '^]'.
# hello
# world
#
# Connection closed by foreign host.
import socket
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.bind(('localhost', 4000))
print "Listening on localhost:4000"
skt.listen(1)
while True:
connection, client_address = skt.accept()
stack = []
try:
# Collect data until blank line is provided
while True:
data = connection.recv(512).strip()
if not data: break
stack.append(data)
# Display data and reset stack
print "\r\n".join(stack)
stack = []
finally:
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment