My question is clear enough. Now, I know the answer.
I know how to inspect source code, I read the source code of method socket.create_connection(address[, timeout[, source_address]]), I asked for source_address, it’s all about binding.
I have this question because I am a beginner, I have no background knowledge of socket programming, so I find a book named “Foundations of Python Network Programming” to read. In chapter 3 TCP, section One Socket per Conversation, i got it and made an example.
import socket
baidu = ('www.baidu.com', 80)
zero = ('0.0.0.0', 9001)
def test(addr, src_addr):
try:
sock = socket.create_connection(addr, 3, src_addr)
print sock.getsockname()
print
sock.sendall('GET / HTTP/1.1\r\n\r\n')
data = sock.recv(1024)
print data
finally:
try:
sock.close()
except:
pass
test(baidu, zero)
#test(baidu, None)
you can toggle the two test functions to see the differences.
3
solved When source_address of python socket.create_connection is used? [closed]