• Python 网络编程-TCP服务端基础篇(一)简单的发送和接收


    #Python 网络编程-TCP服务端基础篇(一)简单的发送和接收
    ##TCP服务端基本流程图

    TCP服务端-单个客户端

     1 import socket
     2 _BUFFSIZE = 1024
     3 ip_addr = r"10.110.36.27"
     4 port = 9000
     5 ip_port = (ip_addr,port)
     6 socket_server = socket.socket()
     7 socket_server.bind(ip_port)
     8 socket_server.listen()
     9 print("正在等待客户端连接")
    10 conn,addr = socket_server.accept()
    11 print("当前连接的ip",addr)
    12 while 1:
    13 print("正在等待接收信息")
    14 receive_msg = conn.recv(_BUFFSIZE)
    15 print("接收到的信息为",receive_msg.decode())
    16 if 'quit' in receive_msg.decode():
    17 break
    18 # send_msg = input("请输入要发送的信息")
    19 conn.sendall(receive_msg)
    20 socket_server.close()

    TCP服务端-多个客户端(二)

     1 import socket
     2 import threading
     3 import time
     4 import re
     5 _BUFFSIZE = 1024
     6 _MAX_CLIENT = 5
     7 ip_addr = r"10.110.36.27"
     8 port = 9000
     9 ip_port = (ip_addr,port)
    10 # socket_server = socket.socket()
    11 # socket_server.bind(ip_port)
    12 # socket_server.listen(_MAX_CLIENT)
    13 # print("正在等待客户端连接")
    14 
    15 # conn,addr = socket_server.accept()
    16 # print("当前连接的ip",addr)
    17 
    18 
    19 class TcpServer(object):
    20     def __init__(self,ip,port):
    21         self._ip = ip
    22         self._port = port
    23 
    24 
    25     def serve_start(self):
    26         self.serve = socket.socket()
    27         self.serve.bind((self._ip,self._port))
    28         self.serve.listen(_MAX_CLIENT)
    29 
    30    
    31     def serve_stop(self):
    32         self.serve.close()
    33 
    34     
    35     # def serve_wait(self):
    36     #     self._con,self._addr = self.serve.accept()
    37     
    38     def con_info(self):
    39         while 1:
    40             print("正在等待客户端连接")
    41             self.con,self.addr = self.serve.accept()
    42             print('当前连接ip',self.addr)
    43             self.client_listen(self.con,self.addr)
    44     
    45     def receive_send(self,conn,addr):
    46         # # while 1:
    47         # conn,addr = self.serve.accept()
    48         # print("当前连接:",addr)
    49         # # print("%s d",addr)
    50         while 1:
    51             # print("来自%s的信息为%s"%(addr,receive_msg))
    52             receive_msg = conn.recv(_BUFFSIZE)
    53             print("来自%s的信息为%s"%(addr,receive_msg))
    54             if receive_msg.decode() =='':
    55                 break
    56             else:
    57                 if 'quit' in receive_msg.decode():
    58                     # conn.close()
    59                     break
    60                 # send_msg = input("请输入要发送的信息")
    61                 conn.sendall(receive_msg)
    62         print("关闭连接:",addr)
    63         conn.close()
    64 
    65     def client_listen(self,conn,addr):
    66         thread_1 = threading.Thread(target=self.receive_send,args=(conn,addr,))
    67         thread_1.start()
    68 
    69 try:
    70     server_obj = TcpServer(ip_addr,port)
    71     server_obj.serve_start()
    72     server_obj.con_info()
    73     # server_obj.client_listen()
    74 finally:
    75     server_obj.serve_stop()
  • 相关阅读:
    对Android开发者有益的40条优化建议
    git推送tag到远端服务器
    详细注释!二维码条码扫描源码,使用Zxing core2.3
    探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
    android的logcat详细用法!
    【Android】开源项目汇总-备用
    android 中theme.xml与style.xml的区别
    Android实现对HOME键的捕获和屏蔽
    java中newInstance()和new()
    java Stack
  • 原文地址:https://www.cnblogs.com/Finding-bugs/p/13731139.html
Copyright © 2020-2023  润新知