Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_connection_refused(self):
# open and close a dummy server to find an unused port
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 0))
server.listen(1)
port = server.getsockname()[1]
server.close()
del server
s = socket.socket()
try:
s.connect(('127.0.0.1', port))
self.fail("Shouldn't have connected")
except socket.error as ex:
code, text = ex.args
assert code in [111, 61, 10061], (code, text)
assert 'refused' in text.lower(), (code, text)
query = ''
if urlo.query != '':
query = '?{}'.format(urlo.query)
req_path = '{}{}'.format(urlo.path, query)
req = '''GET {} HTTP/1.1
Host: {}
User-Agent: runspec/0.9
Accept: {}
'''.format(req_path, urlo.netloc, accept_header)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(30)
sock.connect((ip, urlo.port))
sock.sendall(req)
while len(data) < 15:
data += sock.recv(15)
http_status = data[9:12]
if http_status[0] != '3' or http_status[0] != '2':
http_err += 1
except socket.timeout:
timeout_err += 1
except socket.error as e:
conn_err += 1
finally:
sock.close()
end = time.time()
def setUp(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addr_remote = ('www.python.org.', 80)
self.addr_local = ('127.0.0.1', 25339)
try:
src_rc = self._get_rc_connect()
dst_rc = self._get_rc_connect(dst_san_info)
src_pat = self._get_zfs_volume_name(src['name'])
src_vol_obj = src_rc.get_object(zfsmgr.ZfsDataset(),
radc.ADRGlobPattern({"name":
src_pat}))
dst_pat = dst.rsplit('/', 1)[0]
dst_vol_obj = dst_rc.get_object(zfsmgr.ZfsDataset(),
radc.ADRGlobPattern({"name":
dst_pat}))
send_sock_info = src_vol_obj.get_send_socket(
name=src_snapshot_name, socket_type=zfsmgr.SocketType.AF_INET)
send_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
send_sock.connect((self.hostname, int(send_sock_info.socket)))
dst_san_ip = dst_san_info.split(';')[0]
remote_host, alias, addresslist = socket.gethostbyaddr(dst_san_ip)
recv_sock_info = dst_vol_obj.get_receive_socket(
name=dst, socket_type=zfsmgr.SocketType.AF_INET,
name_options=zfsmgr.ZfsRecvNameOptions.use_provided_name)
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
recv_sock.connect((remote_host, int(recv_sock_info.socket)))
# Set 4mb buffer size
buf_size = 4194304
while True:
# Read the data from the send stream
buf = send_sock.recv(buf_size)
def _get_local_address(node):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((node['ip'], node['port']))
result = s.getsockname()[0]
s.shutdown(socket.SHUT_RDWR)
s.close()
return result
def create_binary(self):
if self.kind == 'ssh':
from wspty.SshTerminal import SshTerminal
return SshTerminal(
self.hostname, self.port, self.username, self.password, self.term
)
if self.kind == 'raw':
from wspty.SocketTerminal import SocketTerminal
sock = eventlet.green.socket.socket()
ip = eventlet.green.socket.gethostbyname(self.hostname)
sock.connect((ip, self.port))
return SocketTerminal(sock)
if self.kind == 'echo':
return EchoTerminal()
if self.kind == 'prompt':
if not self.allow_unsafe:
raise Exception("kind {} is disabled".format(self.kind))
from wspty import PromptTerminal
return PromptTerminal.os_terminal()
raise NotImplemented('kind: {}'.format(self.kind))
def _listen_func(host, port):
# eventlet is setting SO_REUSEPORT by default from v0.20.
# But we can configure it by passing reuse_port argument
# from v0.22
try:
return eventlet.listen((host, port), reuse_port=False)
except TypeError:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(50)
return sock
def _getConnection(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(self.address)
return sock
def connect(self):
self.log.info("fsconnector - connecting to %s %s" % (self.password, str(self.addr)))
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(20.0)
timer = eventlet.timeout.Timeout(20.0)
try:
self.sock.connect(self.addr)
data = self.sock.recv(1024)
if not self.__command('auth %s' % self.password):
self.sock.close()
self.log.error("fsconnector - auth failure")
return False
self.log.info("fsconnector - auth success")
res = self.__set_filter()
if res:
self.log.info("fsconnector - connected")
self.running = True
return res
except eventlet.timeout.Timeout, te: