example tcp socket server

This commit is contained in:
tkl 2016-08-14 04:56:08 +00:00
commit 4aa84b02bf

27
tcp_socket_server.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/python2
import socket
import thread
host = socket.gethostname()
port = 5005
def handler(client, addr):
while True:
data = client.recv(1024)
if not data:
break
print addr, ": ", data
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
while 1:
client, addr = s.accept()
thread.start_new_thread(handler, (client, addr))
if __name__ == "__main__":
main()