V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

python3.7 windows ConnectionAbortedError: [WinError 10053]

  •  1
     
  •   chenqh · 2018-11-30 11:53:17 +08:00 · 2781 次点击
    这是一个创建于 2190 天前的主题,其中的信息可能已经有所发展或是发生改变。

    server 代码

    # coding: utf-8
    import socketserver
    
    
    class Handler_TCPServer(socketserver.BaseRequestHandler):
        """
        The TCP Server class for demonstration.
    
        Note: We need to implement the Handle method to exchange data
        with TCP client.
    
        """
    
        def handle(self):
            # self.request - TCP socket connected to the client
            self.data = self.request.recv(1024).strip()
            print("{} sent:".format(self.client_address[0]))
            print(self.data)
            # just send back ACK for data arrival confirmation
            self.request.sendall("ACK from TCP Server".encode())
    
    if __name__ == "__main__":
        HOST, PORT = "0.0.0.0", 8305
    
        # Init the TCP server object, bind it to the localhost on 9999 port
        tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)
    
        # Activate the TCP server.
        # To abort the TCP server, press Ctrl-C.
        tcp_server.serve_forever()
    

    client 代码:

    # coding: utf-8
    import socket
    from multiprocessing import Process
    
    
    def craw(index):
        host_ip, server_port = "localhost", 8305
        data = "Hello how are you?\n"
    
        # Initialize a TCP client socket using SOCK_STREAM
        tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcp_client.connect((host_ip, server_port))
        for i in range(100):
            # try:
            # Establish connection to TCP server and exchange data
            data = "process:{},index:{},".format(index, i) + data
            tcp_client.sendall(data.encode())
    
            # Read data from the TCP server and close the connection
            received = tcp_client.recv(1024)
            # finally:
            # tcp_client.close()
    
            print("process:{},index:{},Bytes Sent:     {}".format(index, i, data))
            print("process:{},index:{} Bytes Received: {}".format(index, i, received.decode()))
    
    
    if __name__ == '__main__':
        p1 = Process(target=craw, args=(1,))
        p2 = Process(target=craw, args=(2,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        print("Complete")
    
    

    错误

    Traceback (most recent call last):
      File "C:\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
        self.run()
      File "C:\Python37\lib\multiprocessing\process.py", line 99, in run
        self._target(*self._args, **self._kwargs)
      File "F:\Seafile\Nutstore\pycode2\learnqt\tcpcode\tcp_client.py", line 20, in craw
        received = tcp_client.recv(1024)
    ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。
    

    这是什么问题?我使用多进程 socket 的方式有问题?或者应该怎么使用呢?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3475 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:01 · PVG 19:01 · LAX 03:01 · JFK 06:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.