python实现TCP

import socket
import threading

class NetTcp:
    def __init__(self):
        # 创建socket套接字
        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_ip = ''
        server_port = ''
        self.tcp.connect((server_ip, server_port))

        # 创建两个线程
        self.sendThread = threading.Thread(target = self.tcpSend)

    def tcpSend(self):
        print('send')
        send_data = ''
        self.tcp.send(send_data.encode("utf-8"))

    def tcpRecv(self):
        self.tcp.bind(('', 7890))
        self.tcp.listen(128)

        self.newClient, clientAddr = self.tcp.accept()
        recvData = self.newClient.recv(1024)
        # 回送数据给客户端
        self.newClient.send("hahahghai-----ok-----".encode("utf-8"))

    def tcpClosed(self):
        #  5.关闭套接字
        self.tcp.close()
        self.newClient.close()

你可能感兴趣的:(Python,tcp/ip,python,网络)