pythonTCP

1369 단어
TCP 클라이언트
import socket

def main():
    #1、 tcp 
    tcp_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #2、 
    server_ip = "192.168.1.1"
    server_port = 21990
    tcp_socket.connect((server_ip,server_port))
    #3、 
    send_data = ""
    tcp_socket.send(send_data.encode("utf-8"))
    #4、 
    tcp_socket.close()
if __name__ == '__main__':
    main()

TCP 서버
import socket
def main():
    #1、 socket
    tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #2、 
    tcp_server_socket.bind(("10.1.0.245",53780))
    #3、 
    tcp_server_socket.listen(128)
    while True:
        print("wait client...")
        # 4、           
        new_client_socket, client_addr = tcp_server_socket.accept()
        print("get client...")
        while True:
            rev_data = new_client_socket.recv(1024)
            print("client say:%s" % rev_data.decode('utf-8'))
            # rev_data 
            if rev_data:
                new_client_socket.send("go".encode('utf-8'))
            else:
                break



    new_client_socket.send("accept".encode('utf-8'))

    new_client_socket.close()
    tcp_server_socket.close()
if __name__ == '__main__':
    main()

좋은 웹페이지 즐겨찾기