当前位置:   article > 正文

解决requests线程池中,循环调用接口获取数据报错 Failed to establish a new connection: [WinError 10048] 通常每个套接字地址(协议/网络地址

failed to establish a new connection: [winerror 10048] 通常每个套接字地址(协

解决requests线程池中,循环调用接口获取数据报错 Failed to establish a new connection: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次的问题

先上我的代码

import json
import csv
from threadpool import ThreadPool
import threadpool
from threadpool import makeRequests
import time
import random
with open('all_balance.csv','r') as f:
    list1 = csv.reader(f)
    list1 = list(list1)
def load_data(name):
    while True:
        Payload = {
          "id": 0,
          "jsonrpc": "2.0",
          "method": "Filecoin.StateGetActor",
          "params":
          [name[0],[{'/': 'bafy2bzaceazzbdegiso5c4tsjipxjmdabpk5uamj6khzhuwycb4bjeafbhaeo'}, {'/': 'bafy2bzacea4pnuzojwownzajhjdlqitt4wodbvgmhhfc4dujqdkjz3lgl6sac'}, {'/': 'bafy2bzacecjsgkjrcg6bejnqbvm2lktrhxpiazrqwfppueijfzte2bf2kwx42'}, {'/': 'bafy2bzaceaeglquy7h5i6tobxqikaqh2onzvhdjzdhpdneo47grs3qdvvbzc6'}, {'/': 'bafy2bzacea2knbqirjgw7rsfrydo6gfams6wnbpfdrvxhasuo6obqipn4zoco'}, {'/': 'bafy2bzacecmfiu5w7gpuhvdudqpd4qvwng2wokoj6mqydismrujcobgtcunxe'}, {'/': 'bafy2bzacec5mzv2jqqd7k2ripfddlsjv5k2eq52tihjpbtjok37p5hkxep2za'}, {'/': 'bafy2bzacedqyr6oufui2plsbykvevrioa6ukuviyb4i5iz5mq34xxq3gzlz32'}, {'/': 'bafy2bzacecled7zvadjt2jjn354pfhyga22apgqeh5c3ig3vv62tqb6rujsxk'}, {'/': 'bafy2bzacecfyxsfr445b6cvlxnl2p53twzfw4fjqy67bg6nioopb5apa6zb62'}, {'/': 'bafy2bzacech6xyahzbhyyjjd747cyzpllgx4abksncyidxpuxg7hsm2gydxw6'}, {'/': 'bafy2bzaceaisnevf7cpht6cmiwg2l63cqxi5jqyrinjsmdqyvax3delxnj4gg'}]]}
        headers = {"Content-Type": "application/json"}`

        respon = requests.post('http://********:1248/rpc/v0',headers=headers, data=json.dumps(Payload))

        if respon.status_code == 200:
            if respon.json().get('result'):
                print(name[0],int(respon.json()['result']['Balance'])/10**18)
                with open('all_balance_6_24.csv', 'a', encoding='utf-8', newline='') as f:
                    writer = csv.writer(f)
                    writer.writerow([name[0], int(respon.json()['result']['Balance'])/10**18])
                return
        else:
            print(respon.status_code)
            print(respon.text)
            time.sleep(20)
tasks = threadpool.makeRequests(load_data, [list1[i] for i in range(1, 488013)])
pool = threadpool.ThreadPool(100)
for task in tasks:
    pool.putRequest(task)
pool.wait()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

报错代码

Traceback (most recent call last):
  File "E:\project\chain\lib\site-packages\threadpool.py", line 158, in run
    result = request.callable(*request.args, **request.kwds)
  File "E:\project\chain\chain_main_get_state.py", line 22, in load_data
    respon = requests.post('http://10.0.6.22:1248/rpc/v0',headers=headers, data=json.dumps(Payload))
  File "E:\project\chain\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "E:\project\chain\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "E:\project\chain\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "E:\project\chain\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "E:\project\chain\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.0.6.22', port=1248): Max retries exceeded with url: /rpc/v0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000028BAE472C40>: Failed to establish a new connection: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

因为是多线程所谓的关闭python进程不是我想要的办法,最终找到一种有效的办法测试有效:

使用 regedit 命令访问 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/TCPIP/Parameters 注册表子键并创建名为 TcpTimedWaitDelay 的新 REG_DWORD 值。 将此值设置为十进制 30,其为十六进制 0x0000001e。该值将等待时间设置为 30 秒。
使用 regedit 命令访问 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/TCPIP/Parameters 注册表子键并创建名为 MaxUserPort 的新 REG_DWORD 值。 停止并重新启动系统。 缺省值:无 建议值:至少十进制 32768。

我当时在调用接口时,使用命令行工具netstat -n 发现有将近 4000 个到运行接口的目标计算机的 IP 地址的连接处于 TIME_WAIT 状态,则可以增加默认 MaxUserPort 设置并同时降低 TcpTimedWaitDelay 设置,以便不会用尽客户端匿名端口。例如,您可以将 MaxUserPort 设置为 20000,将 TcpTimedWaitDelay 设置为 30。较低的 TcpTimedWaitDelay 设置意味着套接字在 TIME_WAIT 状态中等待的时间更短。较高的 MaxUserPort 设置意味着您可以让更多的套接字处于 TIME_WAIT 状态。
如果你不是很明白步骤可以直接添加我的qq一起交流,希望文章可以帮助到你:377262372
参考文章:https://www.cnblogs.com/tianzhiliang/articles/2400176.html
如何修改注册表:https://jingyan.baidu.com/article/359911f5ac454357fe0306b2.html

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/175675
推荐阅读
相关标签
  

闽ICP备14008679号