(1) build a socket
At first, we need to build a socket, here is an example
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1). socket is the module for internet programming, which is already built in python
2).To build a socket, we need specify type and family.
in here socket.AF_INET is the type, which means the type is IPv4.
SOCK_STREAM is the family, which presents TCP, a stream protocol.
if we want to build an UDP connection, the family should be SOCK_DGRAM.
(2) connection
Then we should do the connection, like this:
s.connect(("www.wxample.com:,80))
what I should remember is ("www.example.com",80) inside the fucntion connect() is atuple, which means is we records this information in an object, it should be const.
What does this tuple consists is ("domain name/ip addr", port number).
(3) utilities
After building the connection, we can use a sort of build-in functions to get informations or handle the connections.
s.getsockname() return the socket's name and port number like this ("10.28.0.11",49533).
s.getpeername() return the peer's name and port number in the same format as getsockname().
send and receive data: there are two ways to send and receive: socket object and file object
socket object: send(), sendto(), recv() and recvfrom(). Which are more easy to use with UDP.
file object: read(), write() and readline(). Which are more easy to use with TCP.