import socket
ip, port = 'localhost', 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
while True:
data = sock.recv(1024)
if not data:
break
print data
import threading, SocketServer
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
'''
Request handler (New thread for each session)
'''
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def __init__(self):
pass
def handle(self):
# Logic for inbound connections here
pass
HOST, PORT = "localhost", 9999
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()