def __init__(self): self.timeout = 30 self.host = self.HOST self.port = self.PORT self.lock = threading.Lock() self.socket = socket.SocketType(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self._connect()
def run(self, bind_ip, bind_port=SERVER_DEFAULT_UDP_PORT): """ Run echo server :type bind_ip: str :type bind_port: int """ try: self.sock_obj = socket.SocketType(AF_INET, SOCK_DGRAM) except IOError as e: print("Failed to create socket: %s" % e.strerror) raise RuntimeError('Can not create socket object') try: self.sock_obj.bind((bind_ip, bind_port)) except IOError as e: errno = e.errno print("Error %d: Failed to bind UDP port %d" % (errno, bind_port)) if 'Windows' == platform.system(): os.system('net helpmsg ' + str(errno)) MSDN_URL = 'https://msdn.microsoft.com/library/windows/desktop/ms740668%28v%3Dvs.85%29.aspx' print('For more information, see MSDN page:') print(MSDN_URL) else: print(e.strerror) self.cleanup() raise RuntimeError('Can not bind UDP port') except: print('Other unexcepted error happened during bind()!') raise default_timeout = 10.0 # in seconds data_buf = bytearray(4096) max_buffer_size = len(data_buf) import select while True: socks_incomming, socks_outgoing, errors = select.select( [self.sock_obj.fileno()], [], [], default_timeout) if len(socks_incomming) <= 0: print('Heartbeat at %s' % time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) continue # When an UDP packet arrives: try: data_len, peer_sock_addr = self.sock_obj.recvfrom_into( data_buf) if data_len > MAX_MESSAGE_SIZE: # Warning: Long messages should be truncated. Drop the extra bytes. data_len = MAX_MESSAGE_SIZE # Send back the data: self.sock_obj.sendto(buffer(data_buf, 0, data_len), peer_sock_addr) except IOError as e: errno = e.errno print("Error %d" % (errno)) print("Debug: max_buffer_size=%d" % (max_buffer_size)) if 'Windows' == platform.system(): os.system('net helpmsg ' + str(errno))
def guarded(*args, **kwargs): if getattr(_module, u'_socket_disabled', False): raise RuntimeError( u"A test tried to use socket.socket without explicitly un-blocking it." ) else: # SocketType is a valid, public alias of socket.socket, # we use it here to avoid namespace collisions return socket.SocketType(*args, **kwargs)
def __init__(self, source, quality=50, ttl=1): try: self.address = '224.0.1.1' self.port = 9000 if source == 'forward' else 9001 self.quality = quality self.sock = sock = socket.SocketType(socket.AF_INET, socket.SOCK_DGRAM, 0) sock.bind(('', 0)) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) self.sequence = 1 except Exception as e: print e
def do_https(v): s = socket.SocketType() s.connect(('kdc.uas.aol.com', 443)) s = ssl.wrap_socket(s) s.write(v) return s
def guarded(*args, **kwargs): if getattr(self._module, '_socket_disabled', False): raise self.exception('Internet is disabled') return socket.SocketType(*args, **kwargs)
print 'TCP Socket now listening on port [' + str(PORT) + ']' except socket.error, msg: print 'Listen failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() i = 0 while 1 and i <= 1: ######################################################################################################################### ##################################Receiving Request and send challenge string############################################ i+=1 if (d): print 'waiting to receive message' readInput, write, error = select.select(sockList,[],[]) request = "" data = "" if (d): print socket.SocketType(readInput) if readInput == udpSock: try: data, clientAddress = udpSock.recvfrom(4096) data = data.split(",") request == data[1] clientID_Request= data[0] except socket.error, msg: print "Request receive failed: ", msg continue elif readInput == tcpSock: conn, addr = tcpSock.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) try: #receive the request from client