Skip to content

Instantly share code, notes, and snippets.

@craig552uk
Created September 12, 2014 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craig552uk/bd8c0d8aaa4d3350d2d2 to your computer and use it in GitHub Desktop.
Save craig552uk/bd8c0d8aaa4d3350d2d2 to your computer and use it in GitHub Desktop.
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