本文整理匯總了Python中socket.getpeername方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.getpeername方法的具體用法?Python socket.getpeername怎麽用?Python socket.getpeername使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socket
的用法示例。
在下文中一共展示了socket.getpeername方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _connect_one
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
def _connect_one(self, host, port):
"""Connect to the proxy and perform a handshake requesting a
connection to (host, port).
Return the open socket on success, or the exception on failure.
"""
client = self.protocol(host, port, self.auth)
sock = socket.socket()
loop = asyncio.get_event_loop()
try:
# A non-blocking socket is required by loop socket methods
sock.setblocking(False)
await loop.sock_connect(sock, self.address)
await self._handshake(client, sock, loop)
self.peername = sock.getpeername()
return sock
except Exception as e:
# Don't close - see https://github.com/kyuupichan/aiorpcX/issues/8
if sys.platform.startswith('linux') or sys.platform == "darwin":
sock.close()
return e
示例2: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
def __init__(self, address, protocol, auth):
"""A SOCKS proxy at an address following a SOCKS protocol. auth is an
authentication method to use when connecting, or None.
address is a (host, port) pair; for IPv6 it can instead be a
(host, port, flowinfo, scopeid) 4-tuple.
"""
self.address = address
self.protocol = protocol
self.auth = auth
# Set on each successful connection via the proxy to the
# result of socket.getpeername()
self.peername = None
示例3: get_client_addr
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
def get_client_addr(self):
return super(IndexHandler, self).get_client_addr() or self.request.\
connection.stream.socket.getpeername()
示例4: sendAck
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
def sendAck(socket):
#print("sending ack to "+str(socket.getpeername()))
ack = FSNObjects.ServerEvent(FSNObjects.ServerEvent.ACK)
send(ack,socket)
示例5: send
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
def send(message, socket):
#print("send()")
#global outboundMessages
#outboundMessages.append({"data":message,"socket":socket})
try:
dataOut = str(message).encode("utf-8")+delim
#print("sending message to client: "+str(socket.getpeername()[0])+": "+str(dataOut))
socket.send(dataOut)
except Exception as e:
print(traceback.format_exc())
socket.close()
# if the link is broken, we remove the client
#remove(socket)
示例6: connectionSuccess
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
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)
示例7: handle_tcp_http
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getpeername [as 別名]
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