Server-side socket_server.py file
#_ *_ coding:utf-8 _*_# Importing Socket module Import socket# Create socket Object SK = Socket.socket () #绑定侦听的IP和端口号ip_port = (' 192.168.9.213 ', 9999) Sk.bind (ip_port) #最大连接数sk. Listen (5) #接受请求, the client's socket object can be obtained when the request is received, and the client's IP and port # through the while loop, allowing the service side to accept the client request print "Waiting for client request" while true:conn, address = Sk.accept () # conn variable stores the client's socket object, the variable address stores the client's IP address and port print "The requested client End, ip:%s Port:%s "% (Address[0], address[1]) #给客户端发一条信息内容 conn.send (' Hello word! ') Conn.close ()
Client socket_client.py file #_*_ coding:utf-8 _*_# importing Socket module Import socket# Create socket Object client = Socket.socket () #连接服务器ip_port = (' 192.168.9.213 ', 9999) client.connect (ip_port) #获取数据data = Client.recv (1024x768) Print data
Test effect
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/8A/C8/wKiom1g71_Tj_BVCAABhkhwcAxw806.png-wh_500x0-wm_3 -wmp_4-s_320120353.png "title=" Sd.png "alt=" Wkiom1g71_tj_bvcaabhkhwcaxw806.png-wh_50 "/>
Test method:
1. Running socket_cliet.py
2. Telnet 192.168.9.213 9999
3, http://192.168.9.213:999
In this way, a socket connection can be established with the server.
This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1877328
Basic flow of Python socket programming