zoukankan      html  css  js  c++  java
  • socketserver.py

    --0--

    import socket
    import os
    server = socket.socket()
    server.bind(('localhost',6971)) #绑定要监听地址和端口  如果是监听所有
    server.listen(5) #监听
    
    print("我要开始等电话了")
    while True:
        conn, addr = server.accept()  # 等电话打进来      conn:服务端生成的实例,接受新实例,addr:客户端的地址
        # conn就是客户端连过来而在服务器端为其生成的一个连接实例
        print(conn, addr)
        print("电话来了")
        # count = 0
        while True:
            data = conn.recv(1024)  #8192
            print("recv:",data)
            if not data:                             ###如果不判断,当client断开之后,server 就会死循环接受空数据.
                print("client has lost...")
                break
            # conn.send(data.upper())
            res = os.popen(data.decode("utf-8")).read()
            print(len(res))
            if len(res)==0:
                res="res is empty"
                # res=b"res has empty"
    
            #res = os.popen(data).read()   ###如果这么写就会报错。
            '''
            Traceback (most recent call last):
              File "D:/PycharmProjects/untitled/day8/sockserver.py", line 22, in <module>
                res = os.popen(data).read()
              File "D:PythonPython36-32libos.py", line 968, in popen
                raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
            TypeError: invalid cmd type (<class 'bytes'>, expected string)
            '''
            conn.send(str(len(res.encode())).encode("utf-8"))             ##发送到client数据的size
            client_ack = conn.recv(1024)                                  ##这两行是为了防止粘包
            print("准备发送了",client_ack.decode("utf-8"))
            conn.send(res.encode("utf-8"))
            # conn.send(res)                ###如果这么写就会报错
            '''
                如果不decode("utf-8")
                conn.send(res)
                TypeError: a bytes-like object is required, not 'str'
            '''
            # count+=1
            # if count >10:break
    
    server.close()
  • 相关阅读:
    032 代码复用与函数递归
    031 实例7-七段数码管绘制
    030 函数的定义与使用
    029 函数和代码复用
    2.4 Buffer
    2.3 字符串链接
    2.2 去除字符串特别字符
    2.1 字符串查询
    存储数据_文件读写
    template模板
  • 原文地址:https://www.cnblogs.com/lwsup/p/7252224.html
Copyright © 2011-2022 走看看