Improve socket behavior
IPv4 socket can not reconnect with the same socket
This commit is contained in:
1 parent
fe5fcecdae
commit
619f424825
2 files changed
+18
-17
No files matched your search
+15
-12
@@ -29,6 +29,7 @@ class PySocket:
|
||||
self.port = None
|
||||
self.ssl = False
|
||||
self.ssl_sock = None
|
||||
self.connected = False
|
||||
|
||||
def connect(self, host, port=False):
|
||||
if not port:
|
||||
@@ -37,18 +38,17 @@ class PySocket:
|
||||
hostname = host
|
||||
if host in DNS_CACHE:
|
||||
host = DNS_CACHE[host]
|
||||
if not self.sock:
|
||||
self.sock = None
|
||||
if socket.has_ipv6:
|
||||
try:
|
||||
socket.getaddrinfo(host, int(port), socket.AF_INET6)
|
||||
self.sock = socket.socket(socket.AF_INET6,
|
||||
socket.SOCK_STREAM)
|
||||
except:
|
||||
pass
|
||||
if self.sock is None:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.settimeout(120)
|
||||
self.sock = None
|
||||
if socket.has_ipv6:
|
||||
try:
|
||||
socket.getaddrinfo(host, int(port), socket.AF_INET6)
|
||||
self.sock = socket.socket(socket.AF_INET6,
|
||||
socket.SOCK_STREAM)
|
||||
except:
|
||||
pass
|
||||
if self.sock is None:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.settimeout(120)
|
||||
self.sock.connect((host, int(port)))
|
||||
DNS_CACHE[hostname], port = self.sock.getpeername()[:2]
|
||||
try:
|
||||
@@ -72,6 +72,7 @@ class PySocket:
|
||||
self.host = host
|
||||
self.hostname = hostname
|
||||
self.port = port
|
||||
self.connected = True
|
||||
|
||||
def disconnect(self):
|
||||
try:
|
||||
@@ -95,6 +96,8 @@ class PySocket:
|
||||
self.sock.close()
|
||||
except:
|
||||
pass
|
||||
self.sock = None
|
||||
self.connected = False
|
||||
|
||||
def reconnect(self):
|
||||
if self.host and self.port:
|
||||
|
||||
+3
-5
@@ -22,9 +22,9 @@ def db_list(host, port):
|
||||
try:
|
||||
if _SOCK and (_SOCK.hostname != host or _SOCK.port != port):
|
||||
_SOCK.disconnect()
|
||||
_SOCK = None
|
||||
if _SOCK is None:
|
||||
_SOCK = pysocket.PySocket()
|
||||
if not _SOCK.connected:
|
||||
_SOCK.connect(host, port)
|
||||
try:
|
||||
_SOCK.send(('db', 'list'))
|
||||
@@ -40,7 +40,6 @@ def db_list(host, port):
|
||||
_SEMAPHORE.release()
|
||||
return res
|
||||
except:
|
||||
_SOCK = None
|
||||
return None
|
||||
|
||||
def db_exec(host, port, method, *args):
|
||||
@@ -50,9 +49,9 @@ def db_exec(host, port, method, *args):
|
||||
try:
|
||||
if _SOCK and (_SOCK.hostname != host or _SOCK.port != port):
|
||||
_SOCK.disconnect()
|
||||
_SOCK = None
|
||||
if _SOCK is None:
|
||||
_SOCK= pysocket.PySocket()
|
||||
if not _SOCK.connected:
|
||||
_SOCK.connect(host, port)
|
||||
_SOCK.send(('db', method) + args)
|
||||
res = _SOCK.receive()
|
||||
@@ -61,7 +60,6 @@ def db_exec(host, port, method, *args):
|
||||
_SEMAPHORE.release()
|
||||
return res
|
||||
except:
|
||||
_SOCK = None
|
||||
raise
|
||||
|
||||
def login(username, password, host, port, database):
|
||||
@@ -73,9 +71,9 @@ def login(username, password, host, port, database):
|
||||
try:
|
||||
if _SOCK and (_SOCK.hostname != host or _SOCK.port != port):
|
||||
_SOCK.disconnect()
|
||||
_SOCK = None
|
||||
if _SOCK is None:
|
||||
_SOCK = pysocket.PySocket()
|
||||
if not _SOCK.connected:
|
||||
_SOCK.connect(host, port)
|
||||
_SOCK.send(('common', 'login', database, username, password))
|
||||
res = _SOCK.receive()
|
||||
|
||||
Reference in new issue
Block a user