#!usr/bin/env python
#-*- coding: utf-8 -*-
#TCP Server
import socket
import threading
bind_ip = "0.0.0.0"#任意地址
bind_port = 9999
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)#最大连接数
print "[*]Listening on %s:%d" % (bind_ip,bind_port)
#客户处理线程
def handle_client(client_socket):
#打印客户端发送得到的内容
request = client_socket.recv(1024)#1k
print "[*]Received: %s" % request
#返还数据包
client_soceket.send("ACK!")
client_soceket.close()
while True:
client,addr = server.accept()
print "[*]Accepted connection from: %s:%d" % (addr[0],addr[1])
#挂起线程处理传入的数据
client_handler = threading.Thread(target=handle_client,args = (client,))
client_handle.start()
转载请注明原文地址: https://ju.6miu.com/read-1294772.html