Forget, chose Python on the way down, pipe right or wrong, since the choice of the front, do not care trials and hardships, today saw the Python socket programming, the real encountered a variety of problems, firewalls, error 10054, 10049, their own on Baidu on various search, Finally came out, the bug out of the feeling that only the most clear. These are the sockets of their own time, some notes:
1. Netstat-no This command is to look at the port, and you can see through the PID which programs are using these ports!
2 in Python or in other languages, using sockets to write server and client communication programs often encounters an error:
Socket.error: [Errno 10048] typically each socket address (Protocol/network address/port) is allowed only once this prompt appears when port conflicts occur, possibly because after creating a socket in the server program opens a port, This socket is not closed at the end of the program, so the next time you start the program you will get this error, workaround: 1, close the socket 2 at the end of the server program, or replace the port number 3, restart the machine
3 ipconfig View the IP of this machine
Client
#clientimport sockethost = ' 10.11.29.186 ' PORT = 8156s = Socket.socket (socket.af_inet,socket. Sock_stream) S.connect ((Host,port)) S.sendall ("Hello world") data = S.recv (1024x768) s.close () print ' Received ', repr (data)
Sever
# sever Programimport sockethost = ' PORT = 8156s = Socket.socket (socket.af_inet,socket. Sock_stream) S.bind ((Host,port)) S.listen (1) conn,addr = s.accept () print ' Connected by ', Addrwhile 1: data = CONN.RECV (1024x768) if not data:break conn.sendall (data) Conn.close ()
The result of the final operation:
Source:
Socket first attempt in Python