python socket之tcp

37 篇文章 2 订阅

python tcp_server端

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import  socket
import time

def send_receive(tcp_socket,list1):
    while True:
        try:
            new_socket, client_addr = tcp_socket.accept()
        except:
            print("not is new client send...")
            time.sleep(5)
        else:
            new_socket.setblocking(False)
            list1.append(new_socket)

        for i in list1:
            try:
                recv_data = i.recv(1024)
            except:
                print("not data send....")
                time.sleep(5)
            else:
                if recv_data != None:
                    print("is data send here...")
                    time.sleep(5)
                else:
                    list1.remove(i)
                    i.close()	

def main():
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tcp_socket.bind(("192.168.8.104", 8088))
    tcp_socket.listen(128)
    tcp_socket.setblocking(False)
    list1 = list()

    send_receive(tcp_socket,list1)
    tcp_socket.close()


if __name__ == "__main__":
    main()

tcp_client端

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket	

def main():
    try:
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_ip = input("请输入服务器的ip: ")
        server_port = int(input("请输入服务器的端口: "))
        ip_port = (server_ip, server_port)
        tcp_socket.connect(ip_port)

        while True:
            num = input("(1)发送数据\t   (2)退出程序\t : ").strip()

            if num == "1":
                send_data = input("请输入你要传的数据: ")
                tcp_socket.send(send_data.encode("utf-8"))
                print("等sever端发数据过来: ")
                servers_data = tcp_socket.recv(1024)
                print("server data is: %s" % servers_data.decode("utf-8") )
            elif num == "2":
                exit()
            else:
                print("输入错误")

    except Exception as result:
        print("unknown error: %s" %result)
    finally:
        tcp_socket.close()

	if __name__ == "__main__":
	    main()

tcp服务端带文件下载功能:

import socket

def down_file_to_client(new_client, address_port):
    while True:  
        recv_file = new_client.recv(1024).decode('gbk')
        if not recv_file:  # 客户在离开时没有数据过来就认为关闭了
            break
        print("clietn %s down file is: %s " % (str(address_port), recv_file))
        files = None
        try:
            f = open(recv_file, "rb")
            files = f.read()
            f.close()
        except Exception as ret:
            print("this is %s not exsit!" % recv_file)
        if files:
            new_client.send(files)
    
def main():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(("192.168.2.121", 8081))
    server_socket.listen(128)
    while True:
        new_client, address_port = server_socket.accept()
        down_file_to_client(new_client, address_port)        
        new_client.close()
    server_socket.close()
    
if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

项目工程师余工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值