• 基于socketserver模块实现并发tcp/udp


    基于socketserver模块实现并发tcp

    客服端

    from socket import *

    client=socket(AF_INET,SOCK_STREAM)
    client.connect(('127.0.0.1',8080))

    while True:
      msg=input(">>: ").strip()
      if len(msg) == 0:
          continue

    client.send(msg.encode('utf-8'))
    data=client.recv(1024)
    print(data.decode('utf-8'))

    服务端

     from socket import *
    #
    #

    # server=socket(AF_INET,SOCK_STREAM)

    # server.bind(('127.0.0.1',8080))

    # server.listen(5)

    #

    # while True:

    #     conn,addr=server.accept()

    #     print(addr)

    #

    #     while True:

    #         try:

    #             data=conn.recv(1024)

    #             if len(data) == 0:break

    #             conn.send(data.upper())

    #         except Exception:

    #             break

    #

    #     conn.close()

    # 支持并发版本

    import socketserver


    class MyRequestHandler(socketserver.BaseRequestHandler):
      def handle(self): # 处理通信
          print(self.client_address)
          while True:
              try:
                  data = self.request.recv(1024) # self.request=>conn
                  if len(data) == 0: break
                  self.request.send(data.upper())
              except Exception:
                  break
          self.request.close()

    if __name__ == '__main__':
      s = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyRequestHandler, bind_and_activate=True)
      s.serve_forever()

    基于socketserver模块实现并发udp

    客服端

    from socket import *

    client = socket(AF_INET, SOCK_DGRAM)

    while True:
      msg = input('>>: ').strip()
      client.sendto(msg.encode("utf-8"), ('127.0.0.1', 8080))

      data, server_addr = client.recvfrom(1024)
      print(data.decode('utf-8'))

    服务端

    import socketserver


    class MyRequestHandler(socketserver.BaseRequestHandler):
      def handle(self): # 处理通信
          data,server=self.request
          server.sendto(data.upper(),self.client_address)

    if __name__ == '__main__':
      s = socketserver.ThreadingUDPServer(('127.0.0.1', 8080), MyRequestHandler, bind_and_activate=True)
      s.serve_forever()

    阿里云部署

    客服端

    from socket import *

    client=socket(AF_INET,SOCK_STREAM)
    client.connect(('121.199.45.113',8080))

    while True:
      msg=input(">>: ").strip()
      if len(msg) == 0:
          continue

    client.send(msg.encode('utf-8'))
    data=client.recv(1024)
    print(data.decode('utf-8'))

    服务端

    # coding:utf-8

    # from socket import *

    #
    #

    # server=socket(AF_INET,SOCK_STREAM)

    # server.bind(('127.0.0.1',8080))

    # server.listen(5)

    #

    # while True:

    #     conn,addr=server.accept()

    #     print(addr)

    #

    #     while True:

    #         try:

    #             data=conn.recv(1024)

    #             if len(data) == 0:break

    #             conn.send(data.upper())

    #         except Exception:

    #             break

    #

    #     conn.close()

    # 支持并发版本

    import socketserver


    class MyRequestHandler(socketserver.BaseRequestHandler):
      def handle(self): # 处理通信
          print(self.client_address)
          while True:
              try:
                  data = self.request.recv(1024) # self.request=>conn
                  if len(data) == 0: break
                  self.request.send(data.upper())
              except Exception:
                  break
          self.request.close()


    if __name__ == '__main__':
      s = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyRequestHandler, bind_and_activate=True)
      s.serve_forever()
    每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)
  • 相关阅读:
    java定时读取文件
    Java面试:投行的15个多线程和并发面试题(转)
    读取一个文件,给定一个字符串,判断这个字符串在文件中出现的次数
    随机产生10个数,并每个数给定一个序号,然后将这10个数按照从小到大的顺序输出来,并带上序号输出
    找出给定字符串中出现最多的字符和次数
    公司开发部门GIT与SVN 之争
    浅谈Hibernate中的三种数据状态
    MyBatis框架的XML数据访问Dao层接口的组合使用
    浅谈WebLogic和Tomcat
    为什么我们不要 .NET 程序员
  • 原文地址:https://www.cnblogs.com/kylin5201314/p/13523609.html
Copyright © 2020-2023  润新知