gpt4 book ai didi

Python - 我怎样才能让客户端能够多次连接?

转载 作者:IT王子 更新时间:2023-10-29 00:35:31 26 4
gpt4 key购买 nike

只有当我使用 client1 = HTTPClient('192.168.1.2', '3') 时它才有效,但是当我同时使用它们时,如下所示:client1 = HTTPClient('192.168.1.2', '3')client2 = HTTPClient('192.168.1.3', '3')

然后整个事情变得非常缓慢,有时其中一个失败。如何保证client1和client2连接+发送+的速度足够快?

import asyncore, socket

class HTTPClient(asyncore.dispatcher):

def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.settimeout(10)
try:
self.connect( (host, 8888) )
except:
print 'unable to connect'
pass
self.buffer = path

def handle_connect(self):
pass

def handle_close(self):
self.close()

def handle_read(self):
print self.recv(8192)

def writable(self):
return (len(self.buffer) > 0)

def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]


client1 = HTTPClient('192.168.1.2', '3')
client2 = HTTPClient('192.168.1.3', '3')
asyncore.loop()

编辑:也尝试了线程但结果相同

import asyncore, socket
import threading
import os

class HTTPClient(asyncore.dispatcher):

def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.settimeout(10)
try:
self.connect( (host, 8888) )
except:
print 'unable to connect'
pass
self.buffer = path

def handle_connect(self):
pass

def handle_close(self):
self.close()

def handle_read(self):
print self.recv(8192)

def writable(self):
return (len(self.buffer) > 0)

def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]

def t1():
client1 = HTTPClient('192.168.1.161', '3')

def t2():
client2 = HTTPClient('192.168.1.163', '3')

t0 = threading.Thread(target=t1())
t0.start()
t0.join()

t0 = threading.Thread(target=t2())
t0.start()
t0.join()

asyncore.loop()

最佳答案

你的 HTTPClient 的 connect 方法是阻塞的,如果你的第二个主机不排除连接,你的 try/except block 在超时后是令人兴奋的。您的 except block 没有关闭套接字,因此 acynscore.loop() 仍在等待它。

在下面尝试我的修改,看看他们是否确认问题。我在 connect 方法周围放置了一些打印语句来说明代码的阻塞部分,并且我在 except 中明确关闭了套接字。如果您可以发布您的服务器代码,这也会有所帮助,因为我预计问题就在那里。如果服务器代码不在你的控制范围内,你的代码将你的超时时间缩短到更短的时间并打开你的连接某种循环,但是你应该在每次迭代时停止你的连接尝试(等待 1 秒,然后 2 然后 4 然后 8,等连接尝试之间)。

import asyncore, socket
import time

class HTTPClient(asyncore.dispatcher):

def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.HOST = host
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.settimeout(10)
try:
t0 = time.time()
print "connecting to host:", host, '...blocking...'
self.connect( (host, 80) )
print "connected to host (%s) in %0.2f seconds:"%(host, time.time()-t0)
except:
print 'unable to connect'
self.close()
self.buffer = path

def handle_connect(self):
pass

def handle_close(self):
self.close()

def handle_read(self):
print self.recv(8192)

def writable(self):
return (len(self.buffer) > 0)

def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]


client1 = HTTPClient('news.google.com', 'GET / HTTP/1.0\r\n\r\n')
client2 = HTTPClient('not_a_valid_server.local', 'GET / HTTP/1.0\r\n\r\n')
client3 = HTTPClient('news.yahoo.com', 'GET / HTTP/1.0\r\n\r\n')
asyncore.loop()

关于Python - 我怎样才能让客户端能够多次连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23050032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com