使用 WebSockets 使用 Tornado 获取客户端端口号

使用 WebSockets 使用 Tornado 获取客户端端口号

问题描述:

我可以获得客户端IP:

I can get client ip:

self.request.remote_ip 

我还可以从 RequestHandler 获取客户端端口号:

I can also get client port number from RequestHandler:

self.request.connection.stream.socket.getpeername()[1]

(感谢这个)

但是来自 WebSocket 的客户端端口号呢?

But what about client port number from WebSocket?

我在文档中找不到有关如何执行此操作的任何信息.我已经通过源代码看了很长时间,但仍然无法弄清楚它在哪里.

I can't find anything on how to do this in the docs. I have had a long look through the source but still can't figure out where it is.

正如我在 如何在RequestHandler中获取客户端端口?,第一个问题是你为什么要这个?

As I said in How to get the client port in RequestHandler?, the first question is why do you want this?

在 websocket 处理程序中,与您在上面发布的代码等效的是 self.stream.socket.getpeername()[1].

In a websocket handler, the equivalent to the code you've posted above is self.stream.socket.getpeername()[1].