Python socket.getpeername() Examples

The following are code examples for showing how to use socket.getpeername(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: luftwaffel   Author: exp-publishing   File: miniircd.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, server, socket):
        self.server = server
        self.socket = socket
        self.channels = {}  # irc_lower(Channel name) --> Channel
        self.nickname = None
        self.user = None
        self.realname = None
        (self.host, self.port) = socket.getpeername()
        self.__timestamp = time.time()
        self.__readbuffer = ""
        self.__writebuffer = ""
        self.__sent_ping = False
        if self.server.password:
            self.__handle_command = self.__pass_handler
        else:
            self.__handle_command = self.__registration_handler 
Example 2
Project: PyCIP   Author: cpchrispye   File: ENIP.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, socket, proto):
        self.host = socket.getsockname()
        self.peer = socket.getpeername()
        self.protocall = proto
        self.recevied_time = time.time() 
Example 3
Project: hostapd-mana   Author: adde88   File: client.py    (license) View Source Project 5 votes vote down vote up
def connectionSuccess(self):
        socket = self.sub.socket
        self.sub.state = "dead"
        del self.sub
        self.state = "connected"
        self.cancelTimeout()
        p = self.factory.buildProtocol(self.buildAddress(socket.getpeername()))
        self.transport = self.transport_class(socket, p, self)
        p.makeConnection(self.transport) 
Example 4
Project: sslstrip-hsts-openwrt   Author: adde88   File: client.py    (license) View Source Project 5 votes vote down vote up
def connectionSuccess(self):
        socket = self.sub.socket
        self.sub.state = "dead"
        del self.sub
        self.state = "connected"
        self.cancelTimeout()
        p = self.factory.buildProtocol(self.buildAddress(socket.getpeername()))
        self.transport = self.transport_class(socket, p, self)
        p.makeConnection(self.transport) 
Example 5
Project: vagus   Author: privacore   File: ClientInterface.py    (license) View Source Project 5 votes vote down vote up
def addresss2nicestring(address):
	#address is a (str,port) tuple from socket.recvfrom() or socket.getpeername()
	#output is something nice such as "192.0.2.7" or "2001:0db8:1::7"
	if address!=None:
		return address[0]
	else:
		return None 
Example 6
Project: honeypot   Author: fabio-d   File: tcp_http_https.py    (license) View Source Project 4 votes vote down vote up
def handle_tcp_http(socket, dstport):
	socket = TextChannel(socket)

	try:
		keep_alive = True
		while keep_alive:
			firstline = readline(socket).strip()
			rematch = re.match("([A-Z]+) ([^ ]+) ?.*", firstline)

			if not rematch:
				raise Exception('Unexpected request')

			verb = rematch.group(1)
			url = rematch.group(2)

			# Skip headers
			keep_alive = False
			user_agent = ''
			while True:
				header = readline(socket).strip()
				if header == '':
					break
				elif header.upper() == 'CONNECTION: KEEP-ALIVE':
					keep_alive = True
				elif header.upper().startswith('USER-AGENT: '):
					user_agent = header[len('USER-AGENT: '):]

			session_token = uuid.uuid4().hex
			log_append('tcp_http_requests', socket.getpeername()[0], dstport, verb, url, user_agent, session_token)

			socket.send("HTTP/1.0 200 OK\nServer: microhttpd (MontaVista/2.4, i386-uClibc)\nSet-Cookie: sessionToken={}; Expires={}\nContent-Type: text/html\nContent-Length: 38\nConnection: {}\n\nmicrohttpd on Linux 2.4, it works!\n\n".format(session_token, __getexpdate(5 * 365 * 24 * 60 * 60), "keep-alive" if keep_alive else "close"))
	except ssl.SSLError as err:
		print("SSL error: {}".format(err.reason))
		pass
	except Exception as err:
		#print(traceback.format_exc())
		pass

	try:
		print("-- HTTP TRANSPORT CLOSED --")
		socket.close()
	except:
		pass 
Example 7
Project: pyNgrok   Author: aaronsh   File: common.py    (license) View Source Project 4 votes vote down vote up
def run(self):
        if self.sck_side_A is not None and self.sck_side_B is not None:
            logger = logging.getLogger("logger")
            logger.info("%s start"%self.getName())
            set_sock_buff_size(self.sck_side_A)
            set_sock_buff_size(self.sck_side_B)
            #send cached data
            if self.cached_data_from_A:
                self.send_all(self.sck_side_B, self.cached_data_from_A)
            if self.cached_data_from_B:
                self.send_all(self.sck_side_A, self.cached_data_from_B)
            scks = (self.sck_side_A, self.sck_side_B)
            while self.forwarding:
                try:
                    readable,writeable,exceptional = select.select(scks,[],scks, 1)
                    for s in readable:
                        data = recv(s, BUFFER_SIZE)
                        if len(data):
                            if s == self.sck_side_A:
                                #self.sck_side_B.sendall(data)
                                self.send_all(self.sck_side_B, data)
                            else:
                                #self.sck_side_A.sendall(data)
                                self.send_all(self.sck_side_A, data)
                        else:
                            # remote closed
                            self.forwarding = False
                            break
                    for s in exceptional:
                        socket.getpeername()
                        peer_addr, peer_port = s.getpeername()
                        local_addr, local_port = s.getsockname(self)
                        logger.error("socket in exceptional %s:%d->%s:%d"%(local_addr, local_port, peer_addr, peer_port))
                        self.forwarding = False
                        break
                except socket.error,e:
                    logger.error(traceback.format_exc())
                    self.forwarding = False
                except Exception, e:
                    logger.error(traceback.format_exc())
                    self.forwarding = False
        if self.sck_side_A is not None:
            safe_close_socket(self.sck_side_A)
        if self.sck_side_B is not None:
            safe_close_socket(self.sck_side_B)
        logger.info("%s end"%self.getName())