Skip to content

Instantly share code, notes, and snippets.

@mike-zhang
Created September 29, 2012 09:45
Show Gist options
  • Save mike-zhang/3803593 to your computer and use it in GitHub Desktop.
Save mike-zhang/3803593 to your computer and use it in GitHub Desktop.
a simple tcp server (python code)
#! /usr/bin/python
# a simple tcp server
import socket,os
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 12345))
sock.listen(5)
while True:
connection,address = sock.accept()
buf = connection.recv(1024)
print buf
connection.send(buf)
connection.close()
@osalbahr
Copy link

osalbahr commented Oct 5, 2025

Thx! And here is an updated version using Python 3:

#!/usr/bin/env python3
# a simple tcp server
import socket,os
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock.bind(('127.0.0.1', 12345))  
sock.listen(5)  
while True:  
    connection,address = sock.accept()  
    buf = connection.recv(1024)  
    print(buf)
    connection.send(buf)    		
    connection.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment