天天看点

[WinError 10038] 在一个非套接字上尝试了一个操作

[WinError 10038] 在一个非套接字上尝试了一个操作

socket先close再调recv就会报错。

解决方法:可以判断一下是否关闭了再操作:

if not tcpCliSock._closed:      
# !/usr/bin.env python
# -*- coding: cp936 -*-
from socket import *
from time import ctime


def serverSocket():
    HOST = 'localhost'  # 主机地址
    PORT = 21567  # 服务器接收端口
    BUFSIZ = 1024  # 接收套接字的缓冲区大小
    ADDR = (HOST, PORT)  # 仅仅是将二者合起来,作为连接到的服务器地址类型

    tcpSerSock = socket(AF_INET, SOCK_STREAM)  # udpSerSock = socket(AF_INET , SOCK_DGRAM)
    tcpSerSock.bind(ADDR)
    tcpSerSock.listen(5)

    while True:
        print('waiting for connection...')
        tcpCliSock, address = tcpSerSock.accept()

        print('connected from :', address)

        while True:
            recvData = tcpCliSock.recv(BUFSIZ)
            if not recvData:
                print('no found data')
                break
            tcpCliSock.send(('[%s] %s' % (ctime(), recvData)).encode('utf-8'))
            tcpCliSock.close()
    tcpSerSock.close()


def main():
    serverSocket()


if __name__ == '__main__':
    main()