Commit 3242a44e authored by Luke Macken's avatar Luke Macken

More robust socket handling

parent 7ccde020
......@@ -83,20 +83,23 @@ class PyrasiteIPC(object):
af, socktype, proto, canonname, sa = res
try:
self.server_sock = socket.socket(af, socktype, proto)
try:
self.server_sock.bind(sa)
self.server_sock.listen(1)
except socket.error:
self.server_sock.close()
self.server_sock = None
continue
except socket.error:
self.server_sock = None
continue
try:
self.server_sock.bind(sa)
self.server_sock.listen(1)
except socket.error:
self.server_sock.close()
self.server_sock = None
continue
break
self.hostname, self.port = self.server_sock.getsockname()[0:2]
self.running = True
if not self.server_sock:
raise Exception('pyrasite was unable to setup a ' +
'local server socket')
else:
self.hostname, self.port = self.server_sock.getsockname()[0:2]
def create_payload(self):
"""Write out a reverse python connection payload with a custom port"""
......
......@@ -61,19 +61,20 @@ class ReverseConnection(threading.Thread, pyrasite.PyrasiteIPC):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
try:
self.sock.connect(sa)
except socket.error:
self.sock.close()
self.sock = None
continue
except socket.error:
self.sock = None
continue
try:
self.sock.connect(sa)
except socket.error:
self.sock.close()
self.sock = None
continue
break
if not self.sock:
raise Exception('pyrasite cannot establish reverse connection to %s:%d' % (self.host, self.port))
raise Exception('pyrasite cannot establish reverse ' +
'connection to %s:%d' % (self.host, self.port))
self.on_connect()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment