TCP/UDP通信及下载器(python实现)

UDP发送

import socket

def main():
    # creat a udp socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # use socket send data
    dest_addr = ('192.168.1.202', 8080)
    while True:
        send_data = input("please input send data")

        if send_data == "exit":
            break
        #udp_socket.sendto(b"hello world", dest_addr)
        udp_socket.sendto(send_data.encode("utf-8"), dest_addr)

    # close socket
    udp_socket.close()

    print("______done______")


if __name__ == '__main__':
    main()

UDP接收

import socket

def main():
    # creat a udp socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # bind local port
    local_addr = ('', 1215)
    udp_socket.bind(local_addr)

    # use socket recieve data
    while True:
        rev_data = udp_socket.recvfrom(1024)
        rev_msg = rev_data[0]
        send_addr = rev_data[1]

        if rev_msg.decode('gbk') == "exit":
            break
        
        print("%s:%s" % (str(send_addr), rev_msg.decode('gbk')))

    # close socket
    udp_socket.close()

    print("______done______")


if __name__ == '__main__':
    main()

TCP服务端

import socket

def main():
    # creat a tcp socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_ip = input("Please Input the Server-IP:")
    server_port = int(input("Please Input the Server-PORT:"))

    # bind the local port
    tcp_server_socket.bind((server_ip, server_port))

    # wait for being connected 
    # listen(128): there is allowed 128 clients connected at the same time
    tcp_server_socket.listen(128)
    clinet_socket, client_Addr = tcp_server_socket.accept()

    # use socket send/recieve data
    while True:

        recvData = clinet_socket.recv(1024)
        print('Recieve Data:', recvData.decode('gbk'))

        send_data = input("please input send data:")

        if send_data == "exit":
            break

        clinet_socket.send(send_data.encode("gbk"))

        

    # close socket
    tcp_server_socket.close()

    print("______done______")


if __name__ == '__main__':
    main()

TCP客户端

import socket

def main():
    # creat a tcp socket
    tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_ip = input("Please Input the Server-IP:")
    server_port = int(input("Please Input the Server-PORT:"))

    # connect the server
    tcp_client_socket.connect((server_ip, server_port))

    # use socket send/recieve data
    while True:
        send_data = input("please input send data:")

        if send_data == "exit":
            break
        #udp_socket.sendto(b"hello world", dest_addr)
        tcp_client_socket.send(send_data.encode("gbk"))

        recvData = tcp_client_socket.recv(1024)
        print('Recieve Data:', recvData.decode('gbk'))

    # close socket
    tcp_client_socket.close()

    print("______done______")


if __name__ == '__main__':
    main()

服务端下载器

import socket
import sys

def send_file_2_client(new_client_socket, client_addr):
    # recieve the file name
    file_name = new_client_socket.recv(1024).decode("utf-8")
    print("client(%s) needs to download the flie: %s" % (str(client_addr), file_name))

    file_content = None
    try:
        f =  open(file_name, "rb")
        file_content = f.read()
        f.close()
    except Exception as ret:
        print("there is no file named:%s" % file_name)

    # send data to client
    if file_content:
        new_client_socket.send(file_content)

def main():
    # creat the socket
    tcp_server_socket = socket(socket.AF_INET, socket.SOCK_STREAM)
    # local messege
    server_ip = input("Please Input the Server-IP:")
    server_port = int(input("Please Input the Server-PORT:"))

    # bind the local port and start to listen
    tcp_server_socket.bind((server_ip, server_port))
    tcp_server_socket.listen(128)

    while True:
        # wait for being connected
        new_client_socket, client_addr = tcp_server_socket.accept()

        # use function to send file to client
        send_file_2_client(new_client_socket, client_addr)
    
        # close socket
        new_client_socket.close()
        
    tcp_server_socket.close()


if __name__ == '__main__':
    main()

客户端下载器

import socket
import sys

def main():

    # creat the socket
    tcp_client_socket = socket(socket.AF_INET, socket.SOCK_STREAM)
    # local messege
    server_ip = input("Please Input the Server-IP:")
    server_port = int(input("Please Input the Server-PORT:"))

    # connect the server
    tcp_client_socket.connect((server_ip, server_port))
    
    # input the downloading file name
    file_name = input("Please input the downloading file name:")

    # send the downloading request
    tcp_client_socket.send(file_name.encode("utf-8"))

    # recieve the data (1MB)
    recv_data = tcp_client_socket.recv(1024*1024)

    # write data into the local file in binary system(wb)
    if recv_data:
        with open("[recieve]" + file_name, "wb") as f:
            f.write(recv_data)

    # close the socket
    tcp_client_socket.close()


if __name__ == '__main__':
    main()
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容