连接到服务器的客户端IP [英] IP of client that connect to server

查看:178
本文介绍了连接到服务器的客户端IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套接字,可以通过它连接到网页并获得一些东西.

I have a socket that I connect by it to a web page and get something.

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('example.ir', 80))
mysock.send('GET http://example.ir/index.php HTTP/1.0\n\n')
while True:
    print"##########################"
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print data

mysock.close()

现在,我想知道,mysock的端口和IP是什么?
什么IP和端口mysocket用于连接到example.ir?

Now, I like to know, what is the port and IP of mysock?
What IP and port mysocket use to connect to example.ir?

推荐答案

要获取mysock的IP和端口,只需执行以下操作:

To get the IP and port of mysock just do:

ip, port = mysock.getsockname()

同样,要获得与它所连接的对等方相同的信息:

Likewise, to get the same information about the peer it is connected to do:

ip, port = mysock.getpeername()  # 'example.ir', 80

这篇关于连接到服务器的客户端IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆