Created
September 12, 2014 08:38
-
-
Save craig552uk/bd8c0d8aaa4d3350d2d2 to your computer and use it in GitHub Desktop.
A netcat type thing in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# > 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