1. Create a socket
For a client program, it takes two steps to build a socket. First, you need to create an actual socket object. Second, you need to connect it to the remote server.
When setting up a socket object, you need to tell the system two things: the type of communication and the Protocol family. The type of communication indicates what protocol is used to transfer the data. Examples of protocols include IPV4 (current Internet standards), IPV6 (future Internet standards), ipx/spx (NetWare), and AFP (Apple file sharing). By far the most common is IPv4.
The protocol family defines how the data is transmitted, and the type of communication is basically: af_inet (and IPv4 correspondence). The protocol family represents the sock_stream of TCP traffic and the SOCK_DGRAM that represents UDP traffic.
For TCP communication, establish a socket connection, typically similar to this code:
1 s = socket.socket (socket.af_inet, socket. SOCK_STREAM)
To connect a socket, you typically need to provide a tuple (tuple) that contains a remote host or IP address and port number. The code is as follows:
1 s.connect (("www.baidu.com", 80))
2. Find the port number
Most do system comes with a list of port numbers, and the Python socket library contains a getservbyname () function that can be queried automatically.
In order to query this list, you need two parameters: protocol name and port number name. The port name is a string, for example: HTTP can be converted to a port number.
1 #!/usr/bin/env python2 ImportSocket3 4 Print "Creating socket ... .."5s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)6 Print "Done ."7 8 Print "looking up port number ..."9Port = Socket.getservbyname ('http','TCP')Ten Print "Done ." One A Print "connecting to remote host on port%d ..."%Port -S.connect (("www.baidu.com", port)) - Print "Done ."
Operation Result:
3. Getting information from the socket
Once you have established a socket connection, you can get some useful information from him:
1 #!/usr/bin/env python2 ImportSocket3 4 Print "Creating socket ... .."5s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)6 Print "Done ."7 8 Print "looking up port number ..."9Port = Socket.getservbyname ('http','TCP')Ten Print "Done ." One A Print "connecting to remote host on port%d ..."%Port -S.connect (("www.baidu.com", port)) - Print "Done ." the - Print "Connected from", S.getsockname () - Print "Connected to", S.getpeername ()
Operation Result:
Run this program to see two new messages. The first shows your own IP address and port number, and the second shows the IP address and port number of the remote machine. For clients, the port number is assigned by the operating system, so you will find that the port number is different each time you run the program.
4. Using socket communication
Python provides two methods: the socket object and the file class object
The socket object provides the interface for the operating system's send (), sendto (), recv (), and Recvfrom () calls. The file class object provides read (), write (), and ReadLine () these more typical Python interfaces
5.socket exception
1. Socket.error related to general I/O and communication issues
2. Socket.gaierror related to query address information
3. Socket.herror related to other address errors
4. Socket.timeout (requires python2.3 or higher) for processing timeouts after settimeout () is raised on a socket
Python network Programming "two" (using TCP)