Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python tcp

#!/usr/bin/python

import socket
import cPickle
import os
import sys
import signal

PORT = 54321

def handle(cs, addr):
	print "Conn from", addr
	cs.sendall("HAI
")

	try:
		l = cPickle.loads(cs.recv(1024))
		s = sum(l)
		cs.sendall("%d
" % s)
	except:
		cs.sendall("fail :(
")


	cs.sendall("bye
")
	cs.close()

signal.signal(signal.SIGCHLD, signal.SIG_IGN)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", PORT))
s.listen(100)


while 1:
	(cs, addr) = s.accept()
	pid = os.fork()

	if pid == 0:
		s.close()
		handle(cs, addr)
		sys.exit(0)

	cs.close()
Source by amateria.smashthestack.org #
 
PREVIOUS NEXT
Tagged: #python #tcp
ADD COMMENT
Topic
Name
9+1 =