import socket, sys port = 80
host = "www.baidu.com" print "Creating socket..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "done" s.connect((host, port))
print "OK"
print "Connected from", s.getsockname()
print "Connected to", s.getpeername()
可以使用getsocketname()返回本地ip地址与端口号,getpeername()返回服务器ip地址与端口号。