def _get_listener(self, options, defaults): key = hash(tuple(options.iteritems())) if key in self.cached_listeners: existing = self.cached_listeners[key] listener_copy = self._copy_listener(existing) self.listeners[key] = listener_copy return listener_copy type = options.get('type', 'tcp') new_listener = None if type in ('tcp', 'udp', 'unix'): if type == 'tcp': interface = options.get('interface', defaults.get('interface')) port = int(options.get('port', defaults.get('port'))) new_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = (interface, port) elif type == 'udp': interface = options.get('interface', defaults.get('interface')) port = int(options.get('port', defaults.get('port'))) new_listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) address = (interface, port) elif type == 'unix': new_listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) address = options.get('path', defaults.get('path')) new_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) new_listener.setblocking(0) new_listener.bind(address) if type != 'udp': backlog = int(options.get('backlog', 256)) new_listener.listen(backlog) elif type == 'custom': new_listener = self._load_from_custom(options) else: raise ValueError('Unknown listener type: {0}'.format(type)) self.listeners[key] = new_listener return new_listener
def test_channel_binding(self): self._create_certificate('server.pem', '/CN=foo/') cb = []; data = [] def server(sock): conn, addr = sock.accept() sslsock = SSLSocket(conn, server_side=True, keyfile='server.pem', certfile='server.pem') cb.append(sslsock.get_channel_binding()) buf = sslsock.read() data.append(buf) conn = sslsock.unwrap() conn.close() def client(sock, addr): sock.connect(addr) sslsock = SSLSocket(sock) cb.append(sslsock.get_channel_binding()) sslsock.write('foo') data.append('foo') sslsock.unwrap() s1 = socket.socket() s1.bind(('localhost', 0)) s1.listen(2) s2 = socket.socket() g1 = gevent.spawn(server, s1) g2 = gevent.spawn(client, s2, s1.getsockname()) gevent.joinall([g1, g2]) s1.close(); s2.close() assert len(cb) == 2 assert len(cb[0]) in (12, 36) assert cb[0] == cb[1] assert len(data) == 2 assert data[0] == data[1]
def _get_local_ips(): local_ips = [] # get local ip using UDP and a broadcast address s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # Not using <broadcast> because gevents getaddrinfo doesn't like that # using port 1 as per hobbldygoop's comment about port 0 not working on osx: # https://github.com/sirMackk/ZeroNet/commit/fdcd15cf8df0008a2070647d4d28ffedb503fba2#commitcomment-9863928 s.connect(('239.255.255.250', 1)) local_ips.append(s.getsockname()[0]) # Get ip by using UDP and a normal address (google dns ip) try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 0)) local_ips.append(s.getsockname()[0]) except: pass # Get ip by '' hostname . Not supported on all platforms. try: local_ips += socket.gethostbyname_ex('')[2] except: pass # Delete duplicates local_ips = list(set(local_ips)) # Probably we looking for an ip starting with 192 local_ips = sorted(local_ips, key=lambda a: a.startswith("192"), reverse=True) return local_ips
def keep_alive(self): print 'keep_alive' #心跳包 #heartbeat_pkt=struct.pack('!B',0x80) while True: #每隔4s 发送一次心跳 sleep(4) try: #self.sock.sendall(heartbeat_pkt) self.pro.encodeData(0x02,'') pass #except error: except: #尝试重连 self.isalive = False self.sock = socket(_socket.AF_INET, _socket.SOCK_STREAM) while not self.isalive: try: self.sock.connect(self.ipp) self.sock.sendall(self.uuidstr) self.pro.setfileobj(self.sock.makefile()) self.isalive = True except error,e: #print e self.sock = socket(_socket.AF_INET, _socket.SOCK_STREAM) self.isalive = False #print 'kkkkk' sleep(1) except: print '2k2k2k2k2' sleep(1)
def __init__(self,service,hostname='',ipv6=False,port=0,backlog=10,reuse_addr=True,authenticator=None,registrar=None,auto_register=None,protocol_config={},logger=None,listener=None): self.active = False self._closed = False self.service = service self.authenticator = authenticator self.backlog = backlog if auto_register is None: self.auto_register = bool(registrar) else: self.auto_register = auto_register self.protocol_config = protocol_config self.clients = set() if listener is None: if ipv6: if hostname == 'localhost' and sys.platform != 'win32': hostname = 'localhost6' self.listener = socket.socket(socket.AF_INET6,socket.SOCK_STREAM) else: self.listener = socket.socket(socket.AF_INET,socket.SOCK_STREAM) if reuse_addr and sys.platform != 'win32': self.listener.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) self.listener.bind((hostname,port)) else: self.listener = listener sockname = self.listener.getsockname() self.host, self.port = sockname[0], sockname[1] if logger is None: logger = logging.getLogger("%s/%d" % (self.service.get_service_name(), self.port)) self.logger = logger if "logger" not in self.protocol_config: self.protocol_config["logger"] = self.logger if registrar is None: registrar = UDPRegistryClient(logger=self.logger) self.registrar = registrar
def __heartbeat_dispatcher(self): """Send out heartbeats, to all hosts.""" while True: heartbeat = None; self.__debug(self.__server_table); if (self.config['SEND_HEARTBEATS']): # Build the heartbeat. if (self.config['IGNORE']): heartbeat = str(self.config['NODE_DL_HOSTNAME'] + ',IGNORE,IGNORE'); else: heartbeat = str(self.config['NODE_DL_HOSTNAME'] + ',' + str(getloadavg()[0] / CPU_CORES) + ',' + str(self.__netload)); # And send it to every host in the list. for host in self.__server_table.keys(): try: if (self.__server_table[host]): if (self.__server_table[host]['yet_to_be_named_metric'] > int(self.config['SERVER_TIMEOUT'])): # If it's been less than SERVER_TIMEOUT seconds since we got a heartbeat... del self.__server_table[host]; # ...delete the host from server_table. continue; else: # Otherwise... self.__server_table[host]['yet_to_be_named_metric'] += 1; # ...update the count since we last heard from them. if (heartbeat): socket.socket(socket.AF_INET, socket.SOCK_DGRAM).sendto(sha256(heartbeat + self.config['SHARED_SECRET']).hexdigest() + heartbeat, (host, int(self.config['HEARTBEAT_PORT']))); except Exception as err: self.__debug("Error sending heartbeat: " + str(err)); sleep(int(self.config['HEARTBEAT_INTERVAL']) / 1000.0);
def childproxy(conn, headers, conn_name='', serv_name=''): if proxy_type == 'http': s = socket.socket() connect_proxy(conn, s, headers, conn_name, serv_name) elif proxy_type == 'https': print('https') sock = ssl.wrap_socket(socket.socket()) connect_proxy(conn, sock, headers, conn_name, serv_name) elif proxy_type == 'socks': method, version, scm, address, path, params, query, fragment = parse_header(headers) s = socket.socket() geventsocks.connect(s, address) print('connect {} success'.format(serv_name)) if headers.startswith('CONNECT'): conn.sendall(b'HTTP/1.1 200 Connection established\r\n\r\n') create_pipe(conn, s, conn_name=conn_name, serv_name=serv_name) else: raw_headers = headers headers = make_headers(headers) s.sendall(headers.encode()) print(conn_name, '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')), raw_headers.split('\r\n')[0]) g = gevent.spawn(from_serv_to_cli, conn, s, conn_name, serv_name) try: from_cli_to_serv(conn, s, conn_name, serv_name) # for buf in iter(lambda:conn.recv(1024*16), b''): # s.sendall(buf) except (BrokenPipeError, ConnectionResetError): print('server: {} client: {} close'.format(serv_name, conn_name) ) g.kill() return
def check_port(port, rais=True, ipv4=True, ipv6=True): """ True -- it's possible to listen on this port for TCP/IPv4 or TCP/IPv6 connections (UNIX Sockets in case of file path). False -- otherwise. """ try: if isinstance(port, (int, long)): if ipv4: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('127.0.0.1', port)) sock.listen(5) sock.close() if ipv6: sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) sock.bind(('::1', port)) sock.listen(5) sock.close() else: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(port) except socket.error as e: if rais: raise RuntimeError( "The server is already running on port {0}".format(port)) return False return True
def BuildSocket(ip): if IPy.IP(ip).version() == 4: s = socket.socket() elif IPy.IP(ip).version() == 6: s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) else: print "There is an invalid string in SSL_Test Func:",ip raise socket.error s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) return s
def __init__(self, addr, timeout=None): if isinstance(addr, basestring): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) elif isinstance(addr, tuple): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) else: raise ValueError('Address must be a tuple or a string not %s', type(addr)) sock.connect(addr) super(ClientConnection, self).__init__(sock)
def TCPInitialize(self, ServerMode, address, port): self._ServerMode = ServerMode self._address = address self._port = port if(self._ServerMode): self._server = socket.socket() self._server.bind((self._address, self._port)) self._server.listen(500) else: self._client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._client.connect((self._address, self._port))
def test_mutiple_clients(self): csock1 = socket.socket() csock1.connect(self.address) client1 = MessageBusConnection(csock1, self.authtok) csock2 = socket.socket() csock2.connect(self.address) client2 = MessageBusConnection(csock2, self.authtok) client1.call_method('set_value', 10) reply = client2.call_method('get_value') assert reply == 10 client1.close() client2.close()
def _connect(self,): """ """ assert self._stream == None or self._stream.closed() if isinstance(self._address, str): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(10) s.connect(self._address) self._stream = IOStream(s) self._stream.read_bytes(struct.calcsize("!I"), self._get_buffer_size)
def connect(self): host = self._host port = self._port if self._fd is None: #try to find the server on the same sub-net if host is None or port is None: udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # go through all interfaces, and issue broadcast on each for addr in ip4_broadcast_addresses(host is None): udp.sendto('Hello',(addr,protocol.DEFAULT_UDP_SERVER_PORT)) timeout = 3. server_found = [] while 1: rlist,_,_ = select.select([udp],[],[],timeout) if not rlist: if port is None: if server_found: msg = "Could not find the conductor on host %s\n" % self._host msg += "But other conductor servers replied:\n" msg += '\n'.join(('%s on port %s' % (host,port) for host,port in server_found)) raise ConnectionException(msg) else: raise ConnectionException("Could not find the conductor") else: break else: msg,address = udp.recvfrom(8192) host,port = msg.split('|') port = int(port) if self._host == 'localhost': localhost = socket.gethostname() if localhost == host: break elif self._host is not None and host != self._host: server_found.append((host,port)) host,port = None,None timeout = 1. else: break self._host = host self._port = port self._fd = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self._fd.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1) self._fd.setsockopt(socket.SOL_IP, socket.IP_TOS, 0x10) self._fd.connect((host,port)) self._raw_read_task = gevent.spawn(self._raw_read) self._cnx = self._fd if posix_ipc: self._g_event.clear() self._fd.sendall(protocol.message(protocol.POSIX_MQ_QUERY,socket.gethostname())) self._g_event.wait(1.)
def test_pair_and_sync(self): # Create two databases and two models database1 = Database(self.tempfile()) model1 = create(Model, database1) assert instance(Model) is model1 vault1 = model1.create_vault('Vault1', 'Passw0rd') database2 = Database(self.tempfile()) model2 = Model(database2) vault2 = model2.create_vault('Vault2', 'Passw0rd', uuid=vault1['id']) # Start a message bus server and client connection lsock = socket.socket() lsock.bind(('localhost', 0)) lsock.listen(2) mbserver = create(MessageBusServer, lsock, 'S3cret', None) #mbserver.set_trace('/tmp/server.txt') mbserver.start() csock = socket.socket() csock.connect(lsock.getsockname()) mbhandler = MBTestHandler() mbclient = MessageBusConnection(csock, 'S3cret', mbhandler) #mbclient.set_trace('/tmp/client.txt') # Start the syncapi lsock = socket.socket() lsock.bind(('localhost', 0)) lsock.listen(2) address = lsock.getsockname() syncapp = SyncAPIApplication() syncapp.allow_pairing = True server = SyncAPIServer(lsock, syncapp) server.start() # Pair with vault1 client = SyncAPIClient(lsock.getsockname()) client.connect() kxid = client.pair_step1(vault1['id'], 'foo') assert kxid is not None assert mbhandler.name == 'foo' certinfo = { 'name': 'node2', 'node': vault2['node'] } keys = certinfo['keys'] = {} for key in vault2['keys']: keys[key] = { 'key': vault2['keys'][key]['public'], 'keytype': vault2['keys'][key]['keytype'] } peercert = client.pair_step2(vault1['id'], kxid, mbhandler.pin, certinfo) assert isinstance(peercert, dict) assert model1.check_certinfo(peercert)[0] model2.add_certificate(vault2['id'], peercert) # Sync version1 = model1.add_version(vault1['id'], {'foo': 'bar'}) client.sync(vault1['id'], model2) version2 = model2.get_version(vault1['id'], version1['id']) assert version2 is not None assert version2['foo'] == 'bar'
def stream_udp(bind_addr, target_addr): # Setup the sockets source = { "socket": socket.socket(socket.AF_INET, socket.SOCK_DGRAM), "addr": None } source["socket"].bind(bind_addr) target = { "socket": socket.socket(socket.AF_INET, socket.SOCK_DGRAM), "addr": target_addr } # Source -> Target _stream_udp(source, target)
def accept(self): if self.protocol == 'ipc': try: os.remove(self.host) except OSError: pass # s.bind(self.host) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind(self.host) os.chmod(self.host, 0770) if self.user: import pwd pe = pwd.getpwnam(self.user) os.chown(self.host, pe.pw_uid, pe.pw_gid) else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((self.host, self.port)) s.listen(self.backlog) self.listeningsock = s #log.info("Listening on %s (%s) ..." % (self.address, self)) # print "Listening on %s (%s) ..." % (self.address, self) exec_count = 0 while True: try: connection, addr = self.listeningsock.accept() connection = Connection(socket.socket(_sock=connection), self._remove_connection) self.connections.add(connection) yield connection, addr except Exception as e: if not self.listeningsock: break #log.exception('Could not accept a connection...') # print 'Could not accept a connection...' # print traceback.print_exc() sleep(0.5 * exec_count) exec_count += 1 else: #log.debug("New connection at %s" % self.address) # print "New connection at %s" % self.address exec_count = 0
def connect_to_resource(self, addr, connect_timeout=None, inactivity_timeout=None): with gevent.Timeout(connect_timeout, ConnectionError): try: if is_ipv6(addr[0]): sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) else: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(addr) except socket.error, e: raise ConnectionError( "socket error while connectinng: [%s]" % str(e))
def main(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(1) while True: conn, addr = s.accept() gevent.spawn(lambda: handle_client(conn, addr))
def setup_tunnel(self): """ Sets up the L2TPv3 kernel tunnel for data transfer. """ try: self.socket = gsocket.socket(gsocket.AF_INET, gsocket.SOCK_DGRAM) self.socket.bind((self.manager.address, self.port)) self.socket.connect(self.endpoint) self.socket.setsockopt(gsocket.IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_PROBE) except gsocket.error: raise TunnelSetupFailed # Setup some default values for PMTU self.pmtu = 1446 self.probed_pmtu = 0 # Make the socket an encapsulation socket by asking the kernel to do so try: self.manager.netlink.tunnel_create(self.id, self.peer_id, self.socket.fileno()) session = self.create_session() except L2TPTunnelExists: self.socket.close() raise except NetlinkError: self.socket.close() raise TunnelSetupFailed
def unwrap(self): if self._sslobj: while True: try: s = self._sslobj.shutdown() break except SSLWantReadError: if self.timeout == 0.0: return 0 self._wait(self._read_event) except SSLWantWriteError: if self.timeout == 0.0: return 0 self._wait(self._write_event) self._sslobj = None # The return value of shutting down the SSLObject is the # original wrapped socket, i.e., _contextawaresock. But that # object doesn't have the gevent wrapper around it so it can't # be used. We have to wrap it back up with a gevent wrapper. sock = socket(family=s.family, type=s.type, proto=s.proto, fileno=s.fileno()) s.detach() return sock else: raise ValueError("No SSL wrapper around " + str(self))
def __init__(self, sock=None, address=None): self.socket = socket.socket() if sock is None else sock self.address = address self.msg_id_counter = 0 self.start_time = time.time() self.ignored_messages = [] self.leftover = ""
def test_connection_refused(self): s = socket() try: s.connect(('127.0.0.1', 81)) except error as ex: assert ex.args[0] == ECONNREFUSED, repr(ex) assert 'refused' in str(ex).lower(), str(ex)
def socket_server(host, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((host, port)) sock.listen(256) while True: client, address = sock.accept() gevent.spawn(socket_accept, client, address)
def perform_m_search(local_ip): """ Broadcast a UDP SSDP M-SEARCH packet and return response. """ search_target = "urn:schemas-upnp-org:device:InternetGatewayDevice:1" ssdp_request = ''.join( ['M-SEARCH * HTTP/1.1\r\n', 'HOST: 239.255.255.250:1900\r\n', 'MAN: "ssdp:discover"\r\n', 'MX: 2\r\n', 'ST: {0}\r\n'.format(search_target), '\r\n'] ) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((local_ip, 0)) sock.sendto(ssdp_request, ('239.255.255.250', 1900)) if local_ip == "127.0.0.1": sock.settimeout(1) else: sock.settimeout(5) try: return sock.recv(2048) except socket.error: raise UpnpError("No reply from IGD using {} as IP".format(local_ip)) finally: sock.close()
def unwrap(self): if self._sslobj: s = self._sslobj_shutdown() self._sslobj = None return socket(_sock=s) # match _ssl2; critical to drop/reuse here on PyPy else: raise ValueError("No SSL wrapper around " + str(self))
def _connect(cls,host='0.0.0.0',port=0,family=socket.AF_INET,socktype=socket.SOCK_STREAM,proto=0,timeout=3,nodelay=False): s = socket.socket(family,socktype,proto) s.settimeout(timeout) s.connect((host,port)) if nodelay: s.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1) return s
def check(self, url, host_info): host_info[self.description()] = None host_ip = socket.getaddrinfo(url, 443)[0][4][0] try: sock = socket.socket() sock.connect((host_ip, 443)) except socket.error: host_info["ssl checked"].set(True) host_info[self.description()] = True return True try: sock = ssl.wrap_socket(sock, ca_certs="/etc/ssl/certs/ca-certificates.crt", cert_reqs=ssl.CERT_NONE, ) cert = sock.getpeercert() except ssl.SSLError: host_info["ssl checked"].set(True) host_info[self.description()] = True return True host_info["ssl checked"].set(False) host_info[self.description()] = False return False
def server_thread(port): global server_run server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(('0.0.0.0', port)) while server_run: try: with Timeout(2, False): (data, from_ip) = server_socket.recvfrom(1024) if data is None: continue ping = ping_parse(data) if ping is None: continue msg = 'Received - Seq: %d, Timestamp: %d' % (ping.seq, ping.timestamp) print colored(msg, 'green') pong = make_pong(ping) server_socket.sendto(pong, from_ip) except timeout: continue except KeyboardInterrupt: server_run = False
def _run(self): MCAST_GRP = '224.1.1.1' MCAST_PORT = 8001 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('', MCAST_PORT)) mreq = struct.pack("=4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) self._ready.set() while not self._complete.is_set(): try: data, address = s.recvfrom(1024) t = data.split('|') if t[0] == 'V': self._manager.vms[t[1]] = time.time()*100 else: self._manager.ternimals[t[1]] = int(time.time())*100 print (t[0], t[1], address[0]) except : self._complete.wait(timeout=1) continue
def login(self, ipaddress, port, user_passwd_pair_list): for user_passwd_pair in user_passwd_pair_list: husername = binascii.b2a_hex(user_passwd_pair[0]) lusername = len(user_passwd_pair[0]) lpassword = len(user_passwd_pair[1]) hpwd = binascii.b2a_hex(user_passwd_pair[1]) address = binascii.b2a_hex(ipaddress) + '3a' + binascii.b2a_hex( str(port)) data = '0200020000000000123456789000000000000000000000000000000000000000000000000000ZZ5440000000000000000000000000000000000000000000000000000000000X3360000000000000000000000000000000000000000000000000000000000Y373933340000000000000000000000000000000000000000000000000000040301060a09010000000002000000000070796d7373716c000000000000000000000000000000000000000000000007123456789000000000000000000000000000000000000000000000000000ZZ3360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y0402000044422d4c6962726172790a00000000000d1175735f656e676c69736800000000000000000000000000000201004c000000000000000000000a000000000000000000000000000069736f5f31000000000000000000000000000000000000000000000000000501353132000000030000000000000000' data1 = data.replace(data[16:16 + len(address)], address) data2 = data1.replace(data1[78:78 + len(husername)], husername) data3 = data2.replace(data2[140:140 + len(hpwd)], hpwd) if lusername >= 16: data4 = data3.replace('0X', str(hex(lusername)).replace('0x', '')) else: data4 = data3.replace('X', str(hex(lusername)).replace('0x', '')) if lpassword >= 16: data5 = data4.replace('0Y', str(hex(lpassword)).replace('0x', '')) else: data5 = data4.replace('Y', str(hex(lpassword)).replace('0x', '')) hladd = hex(len(ipaddress) + len(str(port)) + 1).replace('0x', '') data6 = data5.replace('ZZ', str(hladd)) data7 = binascii.a2b_hex(data6) try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(self.timeout) s.connect((ipaddress, port)) except Exception as E: logger.debug('ConnectException: {} {} {}'.format( E, ipaddress, port)) return try: s.send(data7) if 'master' in s.recv(1024): log_success("MSSQL", ipaddress, port, user_passwd_pair) else: logger.debug('AuthenticationFailed') except Exception as E: logger.debug('AuthenticationException: %s' % E) continue
def test_proxy(self): self.test_input = 'Hiya, this is a test' mock_service = StreamServer(('127.0.0.1', 0), self.echo_server) gevent.spawn(mock_service.start) gevent.sleep(1) proxy = Proxy('proxy', '127.0.0.1', mock_service.server_port) server = proxy.get_server('127.0.0.1', 0) gevent.spawn(server.start) gevent.sleep(1) s = socket() s.connect(('127.0.0.1', server.server_port)) s.sendall(self.test_input) received = s.recv(len(self.test_input)) self.assertEqual(self.test_input, received) mock_service.stop(1)
def test_proxy(self): self.test_input = "Hiya, this is a test".encode("utf-8") mock_service = StreamServer(("127.0.0.1", 0), self.echo_server) gevent.spawn(mock_service.start) gevent.sleep(1) proxy = Proxy("proxy", "127.0.0.1", mock_service.server_port) server = proxy.get_server("127.0.0.1", 0) gevent.spawn(server.start) gevent.sleep(1) s = socket() s.connect(("127.0.0.1", server.server_port)) s.sendall(self.test_input) received = s.recv(len(self.test_input)) self.assertEqual(self.test_input, received) mock_service.stop(1)
def run_client(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.connect(('localhost', 2786)) session = Session(sock.makefile('rw'), True) start = time.time() # [beam(session) for _ in range(NUM_CALLS)] parallel(session) dt = time.time() - start print('call: %d KB/s, dt=%.4f' % (NUM_CALLS * SZ / dt / 1024, dt)) print "client: active strems", session.stream_count print "client: SessionClose and await exit" session.close() gevent.sleep(0.1) print "client: final stream count", session.stream_count
def run(self): while True: try: self.socket = socket.socket(family=socket.AF_UNIX) self.socket.connect("/var/run/devd.xml.pipe") f = self.socket.makefile("rb", 0) while True: line = self.read_until_nul(f) if line is None: # Connection closed - we need to reconnect # return raise OSError('Connection closed') event = self.__tokenize(line.decode('utf-8', 'replace')) if not event: # WTF continue if "system" not in event: # WTF continue if event["system"] == "DEVFS": self.__process_devfs(event) if event["system"] == "IFNET": self.__process_ifnet(event) if event["system"] == "ZFS": self.__process_zfs(event) if event["system"] == "ISCSI": self.__process_iscsi(event) if event["system"] == "SYSTEM": self.__process_system(event) except OSError as err: # sleep for a half a second and retry self.dispatcher.logger.info( '/var/run/devd.pipe read error: {0}'.format(str(err))) self.dispatcher.logger.info('retrying in 1s') time.sleep(1) self.socket.close()
def getLocalIP(): ''' Get the local IP address as a string Derived from https://github.com/n8henrie/fauxmo ''' hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) # Workaround for Linux returning localhost # See: SO question #166506 by @UnkwnTech if ip_address in ['127.0.1.1', '127.0.0.1', 'localhost']: tempsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) tempsock.connect(('8.8.8.8', 0)) ip_address = tempsock.getsockname()[0] tempsock.close() return ip_address
def init(self, host="172.16.16.41", port=6000): self.host = host self.port = int(port) # self.soc = socket(AF_INET, SOCK_STREAM) self.soc = socket() self.data = b'' self.tempData = None ADDR = (self.host, self.port) try: self.soc.connect(ADDR) except Exception as err: raise else: # self.soc.settimeout(20) self.soc.setblocking(0) # 设置为非阻塞 self.greenlet.spawn(self.__heart) self.greenlet.spawn(self.__loopCheck)
def is_port_open(host, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(3.0) if s.connect_ex((host, int(port))) == 0: return True else: return False except Exception as e: return False finally: try: s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) s.close() except Exception as e: pass
def __init__(self, discovery_port, delay=1): #print("port discovery ",self._etc["DISCOVERY_port"]) self._PROC["dsc_client"] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._PROC["dsc_client"].setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._PROC["dsc_client"].setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self._PROC["dsc_client"].settimeout(delay) self._etc["dsc_delay"] = delay self.discovery_port = discovery_port #print("discovery on port:",discovery_port) self._PROC["dsc_server"] = DatagramServer(('', self.discovery_port), handle=self._receive) self._PROC["dsc_server"].start() #print(self.__dict__) self._PROC["dsc_enabled"] = False
def __init__(self, ac, msgCallback): ''' :param PHPAgentConf ac: ''' self.address = ac.Address if os.path.exists(self.address): os.remove(self.address) self.listen_socket = gsocket.socket(gsocket.AF_UNIX, gsocket.SOCK_STREAM) self.listen_socket.bind(self.address) self.listen_socket.listen(256) self.server = StreamServerLayer(self.listen_socket, self._recvData, self._phpClientSayHello) self.msgHandleCallback = msgCallback self.hello_cb = []
def graphite_worker(host, port): """The worker pops each item off the queue and sends it to graphite.""" sock = socket() try: sock.connect((host, port)) except Exception as e: LOG.error("Failed to connect to Graphite at {0}:{1}".format( host, port)) LOG.error("{0}".format(e)) return LOG.info("Connected to graphite at {0}:{1}".format(host, port)) while True: data = graphite_queue.get() # print "graphite_worker: got data {0!r}".format(data) sock.sendall(data)
def connect(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(self.timeout) try: self.sock.connect((self.host, self.port)) except socket.timeout: raise NotConnectedError('Connection timed out after %s seconds' % self.timeout) self.connected = True self.sock.settimeout(None) self.sock_file = self.sock.makefile() self.start_event_handlers() self._auth_request_event.wait() if not self.connected: raise NotConnectedError('Server closed connection, check ' 'FreeSWITCH config.') self.authenticate()
def test_do_TRACE(self): """ Objective: Test the web server with a trace request """ # requests has no trace method.. So resorting to the good'ol socket - sending raw data s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("127.0.0.1", self.http_server.server_port)) s.sendall(b"TRACE /index.html HTTP/1.1\r\nHost: localhost\r\n\r\n") data = s.recv(1024) # FIXME: Omitting the time etc from data - mechanism to check them needed as well? self.assertIn(b"HTTP/1.1 200 OK", data) # test for 501 - Disable TRACE method self.http_server.cmd_responder.httpd.disable_method_trace = True s.sendall(b"TRACE /index.html HTTP/1.1\r\nHost: localhost\r\n\r\n") data = s.recv(1024) s.close() self.assertIn(b"501", data)
def control_loop(opts): handler = ControlHandler(opts) address = opts.control_socket _log.debug("address from options: {}".format(address)) if address[:1] == '@': address = '\00' + address[1:] sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0) status = sock.bind(address) _log.debug("Binding to address: {}".format(address)) _log.debug("Status of bind: {}".format(str(status))) sock.listen(5) while True: client, client_address = sock.accept() if _verify_request(opts, client, client_address): gevent.spawn(_handle_request, client, handler) else: client.close()
def main_task(): server_addr = ('0.0.0.0', 8888) tcp_server = socket() # tcp_server.setsockopt(socket.SOL_SOCKET, # socket.SO_REUSEADDR, 1) tcp_server.bind(server_addr) tcp_server.listen() # 导入多线程 from threading import Thread while True: user_socket, addr = tcp_server.accept() print('addr=', addr) # 创建协程greenlet对象 p = gevent.spawn(process_client_request, user_socket) # 在此处回收已经死了的线程 is_alive() 来判断 tcp_server.close()
def connect(self): """\ Connect to the IRC server using the nickname """ self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self._sock.connect((self.server, self.port)) except socket.error: self.logger.error('Unable to connect to %s on port %d' % (self.server, self.port), exc_info=1) return False self._sock_file = self._sock.makefile() self.register_nick() self.register() return True
def send_message(self, address, message): """ Send a message """ from gevent import socket message_json = message.json() # Send message sock = socket.socket(type=socket.SOCK_DGRAM) sock.connect(address) sock.send(message_json) try: data, address = sock.recvfrom(8192) # Read 8KB #self.logger.info("[{source}] replied: '{resp}'".format( # source=self.source_format(address), resp=data)) except Exception, e: self.logger.error("[{source}] replied: {error}".format( source=self.source_format(address), error=e))
def try_to_connect(self): print "Trying to connect" # use a new socket when attempting to reconnect! self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) i = 2 while True: try: a = self.client_socket.connect((self.host, self.port)) print "Connected...", a break except: print "Retrying connecting..." gevent.sleep(i) i += 0.1 max_period = 2 i = max_period if i > max_period else i
def wrapped(self, command, client_socket, *args, **kwargs): current_thread = threading.current_thread() tid = current_thread.name.rsplit('-', 1)[-1] current_thread.name = '%s-%s-%s' % (self.client_id[:14], command.cmd, tid) # Create a gevent socket for this thread from the other tread's socket # (using the raw underlying socket, '_sock'): client_socket = socket.socket(_sock=client_socket._sock) self.client_socket = client_socket try: command.executed(func(self, *args, **kwargs)) except (IOError, RuntimeError, socket.error) as e: command.error(e) except DeadException: command.cancelled()
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True, connect_timeout=socket.getdefaulttimeout()): self.port = port self.host = host if parse_yaml: try: import yaml except ImportError: parse_yaml = False self.parse_yaml = parse_yaml self.server_errors = ["OUT_OF_MEMORY", "INTERNAL_ERROR", "BAD_FORMAT", "UNKNOWN_COMMAND"] self._connect_timeout = connect_timeout self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connect()
def start(self, tcp_port): try: self._tcp_server = socket.socket() self._tcp_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._tcp_server.bind(('', tcp_port)) self._tcp_server.listen(100) except socket.error as msg: self._tcp_server.close() logger.error("cannot start due to socket error: %s" % msg) return False self._greenlets = [gevent.spawn(f) for f in self._spawn_handlers] gevent.sleep(0) # Make the greenlet start first and return logger.info("greenlet spawn and TCP server listens on %s:%s" % self._tcp_server.getsockname()[:2]) return True
def __init__(self, counter, host, port): self.sock = socket.socket() self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.sock.bind((host, port)) self.sock.listen(24511) for process_id in range(counter): p1 = Process(target=self.handle_process, args=( self.sock, process_id, )) p1.start() print("Server started at http://%s:%s Running core:%s" % (host, port, counter))
def test(self): sock = socket.socket() sock.connect(('127.0.0.1', self.server_port)) try: sock.settimeout(0.1) try: result = sock.recv(1024) raise AssertionError( 'Expected timeout to be raised, instead recv() returned %r' % (result, )) except socket.error: ex = sys.exc_info()[1] self.assertEqual(ex.args, ('timed out', )) self.assertEqual(str(ex), 'timed out') self.assertEqual(ex[0], 'timed out') finally: sock.close()
def graphite_worker(): """The worker pops each item off the queue and sends it to graphite.""" print 'connecting to graphite on (%s, %s)' % (HOST, PORT) sock = socket() try: sock.connect((HOST, PORT)) except Exception as e: raise Exception( "Couldn't connect to Graphite server {0} on port {1}: {2}".format( HOST, PORT, e)) print 'done connecting to graphite' while True: data = graphite_queue.get() print "graphite_worker: got data {0!r}".format(data) print "sending data" sock.sendall(data)
def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None): """Retrieve the certificate from the server at the specified address, and return it as a PEM-encoded string. If 'ca_certs' is specified, validate the server cert against it. If 'ssl_version' is specified, use it in the connection attempt.""" host, port = addr if (ca_certs is not None): cert_reqs = CERT_REQUIRED else: cert_reqs = CERT_NONE s = wrap_socket(socket(), ssl_version=ssl_version, cert_reqs=cert_reqs, ca_certs=ca_certs) s.connect(addr) dercert = s.getpeercert(True) s.close() return DER_cert_to_PEM_cert(dercert)
def send_udp_request(self, host, port, payload, timeout): """Send udp payloads by port number. """ data = '' try: with contextlib.closing(socket.socket(AF_INET, SOCK_DGRAM)) as client: client.settimeout(timeout) client.sendto(payload, (host, port)) while True: _, addr = client.recvfrom(SOCKET_READ_BUFFERSIZE) if not _: break data += _ except Exception as err: return None return data
def get_socket(self, level='', flag=True): """ 与域名WHOIS信息注册商进行连接查询,level表示顶级或者二级查询,flag表示是否需要添加"="标志 Args: level: 查询WHOIS顶级注册商(top),还是二级注册商(second) flag: 是否需要在查询域名前增加标志位'=',True不添加,False为添加 Returns: data_result: 返回在域名注册商查询所得到的结果 Exception: 1、socket错误,连接或发送造成错误 异常则返回False """ if flag: #判断是否需要包含标志位'=' query_domain = self.domain # 无flag else: query_domain = '=' + self.domain # 有'=' # 顶级、二级域名查询 if level == 'top': if type(self.top_whois_server) == list: # 若WHOIS注册商为列表,则随机选择一个 HOST = choice(self.top_whois_server) else: HOST = self.top_whois_server elif level == 'second': HOST = self.sec_whois_server data_result = '' PORT = 43 BUFSIZ = 1024 ADDR = (HOST, PORT) EOF = "\r\n" data_send = query_domain + EOF try: tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpCliSock.connect(ADDR) tcpCliSock.settimeout(15) tcpCliSock.send(data_send) except socket.error, e: # print 'Socket Wrong' print e tcpCliSock.close() return False
def main(self, name, mesh, host, port, parent): """.""" state = 'unconnected' sock = None to = 1 up = False dataq = queue.Queue() last_heard_from = None while True: if state == 'unconnected': if sock is None: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setTimeout(0.1) try: sock.connect((host, port)) except (socket.error, gevent.Timeout): to = to + random.random() * self.variance else: state = 'connected' receiver = gevent.spawn_link(self.recv, sock, dataq) sender = gevent.spawn_link(self.send, sock, dataq) to = 0 to = to + random.random() * self.variance if up and state != 'connected': if last_heard_from try: pat, msg = self.receive(self.DIE, self.DATA, timeout=to) except gevent.LinkedExited: # Our receiver died for some reason. state, sock = 'unconnected', None continue else: if pat is self.DIE: # The endgame. Someone told us to die. break elif pat is self.DATA: pass
def _tcp_listener(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): """A shortcut to create a TCP socket, bind it and put it into listening state.""" sock = socket(family=family) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise sock.listen(backlog) sock.setblocking(0) return sock
def ping(self, addr='localhost', port=53): """ js9 'print(j.servers.dns.ping(port=53))' """ address = (addr, port) message = b'PING' sock = socket.socket(type=socket.SOCK_DGRAM) sock.connect(address) print('Sending %s bytes to %s:%s' % ((len(message), ) + address)) sock.send(message) try: data, address = sock.recvfrom(8192) except Exception as e: if "refused" in str(e): return False raise RuntimeError("unexpected result") return True