Basic flow of Python socket programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.