重复的 POST 请求导致错误“socket.error: (99, 'Cannot assign requested address')"

2022-01-19 00:00:00 python network-programming

问题描述

我的盒子里部署了一个网络服务.我想通过各种输入检查此服务的结果.这是我正在使用的代码:

I have a web-service deployed in my box. I want to check the result of this service with various input. Here is the code I am using:

import sys
import httplib
import urllib

apUrl = "someUrl:somePort"

fileName = sys.argv[1]
conn = httplib.HTTPConnection(apUrl)

titlesFile = open(fileName, 'r')

try:
    for title in titlesFile:

        title = title.strip()
        params = urllib.urlencode({'search': 'abcd', 'text': title})
        conn.request("POST", "/somePath/", params)
        response = conn.getresponse()
        data = response.read().strip()
        print data+"	"+title

        conn.close()

finally:
    titlesFile.close()

此代码在打印相同数量的行后出现错误 (28233).错误信息:

This code is giving an error after same number of lines printed (28233). Error message:

Traceback (most recent call last):
  File "testService.py", line 19, in ?
    conn.request("POST", "/somePath/", params)
  File "/usr/lib/python2.4/httplib.py", line 810, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.4/httplib.py", line 833, in _send_request
    self.endheaders()
  File "/usr/lib/python2.4/httplib.py", line 804, in endheaders
    self._send_output()
  File "/usr/lib/python2.4/httplib.py", line 685, in _send_output
    self.send(msg)
  File "/usr/lib/python2.4/httplib.py", line 652, in send
    self.connect()
  File "/usr/lib/python2.4/httplib.py", line 636, in connect
    raise socket.error, msg
socket.error: (99, 'Cannot assign requested address')

我正在使用 Python 2.4.3.我也在做 conn.close() .但是为什么会出现这个错误呢?

I am using Python 2.4.3. I am doing conn.close() also. But why is this error being given?


解决方案

这不是python问题.

This is not a python problem.

在 linux 内核 2.4 中,临时端口范围是从 32768 到 61000.因此可用端口数 = 61000-32768+1 = 28233.据我了解,因为所讨论的 Web 服务非常快(<5ms实际上)因此所有端口都用完了.程序必须等待大约一两分钟才能关闭端口.

In linux kernel 2.4 the ephemeral port range is from 32768 through 61000. So number of available ports = 61000-32768+1 = 28233. From what i understood, because the web-service in question is quite fast (<5ms actually) thus all the ports get used up. The program has to wait for about a minute or two for the ports to close.

我所做的是统计 conn.close() 的数量.当数字为 28000 时,等待 90 秒并重置计数器.

What I did was to count the number of conn.close(). When the number was 28000 wait for 90sec and reset the counter.

相关文章