简体   繁体   English

Python:无法建立连接,因为目标机器主动拒绝它

[英]Python: No connection could be made because the target machine actively refused it

I having a multi threaded python program, It will continuously open connection with a local URL( http://testing.com/sendMessages_mock.php ), I have installed the WAMP and having sendMessages_mock.php file in local for testing purpose.我有一个多线程 python 程序,它会持续打开与本地 URL( http://testing.com/sendMessages_mock.php )的连接,我已经安装了 WAMP 并在本地有 sendMessages_mock.php 文件用于测试目的。 Sometimes, I get "[Errno 10061] No connection could be made because the target machine actively refused it"有时,我收到“[Errno 10061] 无法建立连接,因为目标机器主动拒绝它”

Each thread will call this function.每个线程都会调用这个函数。 For every one second, there will hundreds of thread calling this function.每一秒,都会有数百个线程调用这个函数。 Getting this exception only for few thread calls.仅针对少数线程调用获得此异常。 Any thoughts on this ?对此有何想法?

def sendMessage(body):
    try:
        request = Request(url="http://testing.com/sendMessages_mock.php", data=json.dumps(body))
        request.add_header('Authorization', 'key=' + CONST.API_KEY)
        request.add_header('Content-Type', 'application/json')
        response = urlopen(request)
        responseData = response.read().decode('utf-8')

    except Exception as exceptionErr:
        print("Oops! " + str(traceback.format_exc()))

If too many calls per second are a problem that causes WAMP to refuse requests and you'd like to solve this on Python side, you could write limiter class which would handle sendMessage execution and limit number of calls.如果每秒调用过多是导致 WAMP 拒绝请求的问题,并且您想在 Python 端解决此问题,则可以编写限制器类来处理 sendMessage 执行并限制调用次数。

Mind you this is my first answer to someone else's question, so it might be absolutely wrong.请注意,这是我第一次回答别人的问题,所以可能完全错误。 Please have reasonable amount of scepticism.请保持合理的怀疑态度。

#python 2.7
import threading

class Throttle(object):
    def __init__(self, call_limit, interval):
        self.call_limit = call_limit
        self.interval = interval
        self.cleaner = None
        self.buffer = threading.Semaphore(call_limit)
        self.calls_in_buffer = 0

    def call(self, function, *args):
        self.buffer.acquire()

        self.calls_in_buffer += 1
        try:
            return function(*args)
        except:
            raise
        finally:
            if self.cleaner == None:
                self._init_cleaner()

    def _drain(self):
        for i in range(self.call_limit):
            self.calls_in_buffer -= 1
            self.buffer.release()
        self.cleaner = None

    def _init_cleaner(self):
        self.cleaner = threading.Timer(self.interval, self._drain)
        self.cleaner.daemon = True
        self.cleaner.start()

Making calls through such class would effectively throttle requests, so server will be able to handle them.通过此类进行调用将有效地限制请求,因此服务器将能够处理它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-无法建立连接,因为目标计算机主动拒绝了它 - Python - No connection could be made because the target machine actively refused it 无法建立连接,因为目标机器主动拒绝它 Python - No connection could be made because the target machine actively refused it Python Django Python - 无法建立连接,因为目标机器主动拒绝它 - Django Python - No connection could be made because the target machine actively refused it WinError 10061 由于目标机器主动拒绝,无法建立连接 - WinError 10061 No connection could be made because the target machine actively refused it “[WinError 10061] 无法建立连接,因为目标机器主动拒绝它” - "[WinError 10061] No connection could be made because the target machine actively refused it" EmailMultiAlternatives无法建立连接,因为目标计算机主动拒绝了它 - EmailMultiAlternatives no connection could be made because the target machine actively refused it 无法建立连接,因为目标计算机主动拒绝它 - No connection could be made because the target machine actively refused it 无法建立连接,因为目标机器主动拒绝它(Django) - No connection could be made because the target machine actively refused it (Django) Mysql Python 连接器 由于目标机器主动拒绝,无法建立连接 - Mysql Python Connector No connection could be made because the target machine actively refused it Python 中 TCP 连接的简单套接字编程练习; 错误:“无法建立连接,因为目标机器主动拒绝它” - simple socket programming exercise for TCP connections in Python; Error: "No connection could be made because the target machine actively refused it"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM