1. Specific connection process I'm not going to say it. See also: http://woodpecker.org.cn/
2. Use the server-side and client-side communication examples to illustrate
3. On the Code
Service side (server.py)
1 #-*-coding:utf-8-*-2 ImportSocket3host="127.0.0.1"4port= 99995s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)6 S.bind ((host,port))7S.listen (1)8 while1:9Conn,addr=s.accept ()Tendata = CONN.RECV (1024) One ifdata = ='Bye': A Break - Else: - PrintData theTosend = Raw_input (">") - conn.send (tosend) - ifTosend = ='Bye': - Break +S.close ()
View Code
Client (client.py)
1 #-*-coding:utf-8-*-2 ImportSocket3s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)4Host ='127.0.0.1' 5Port = 99996 S.connect ((host,port))7 while1:8Tosend = Raw_input (">")9 s.send (tosend)Ten ifTosend = ='Bye': One Break Adata = S.RECV (1024) - ifdata = ='Bye': - Break the Else: - PrintData -S.close ()
View Code
4. When you run the test, first open the server side, and then you understand.
5.
#编写过程中遇到的问题在端口 (PORT) and hostname (host) are bound to note that the parentheses are two levels, or error, the host's single or double quotation marks do not matter
#端口要对应
Python Network programming