Python's network programming socket programming

Source: Internet
Author: User

Socket is a way of inter-process communication, and other processes of communication between the main difference is: to achieve inter-process communication between different hosts, the network of a variety of services are based on the socket to complete the communication, to solve the network of two host communication problems, the first to uniquely identify the process, In TCP/IP network protocol, it is through (IP address, protocol, port number) ternary group to identify the process, solve the process identity problem, there is the basis of communication

TCP is a connection-oriented Transport layer protocol, TCP socket is based on a client-server programming model, the server listens to the client's connection request, once the connection can be transmitted data, then the introduction of TCP socket programming is also divided into the client and server.

Client Programming:

1. Create socket;2. Connect to a remote server, 3. Send data, 4. Receive data; 5. Close socket

When we open the www.baidu.com, the browser is doing is the above 5 steps, in the socket has this behavior characteristic is called the client, the clients mainly connects the remote system and obtains the data;

(another behavior in the socket is called server, the server uses the socket to receive the connection and provides the data, and the client is the opposite, so www.baidu.com is an HTTP server, the browser is an HTTP client)

# _*_ Encoding:utf-8 _*_import socket
#1. Create sockets = Socket.socket () host = Socket.gethostname () port = 12345#2. Establish a link to the server S.connect ((host,port)) #3. Receive Data print ( S.RECV (1024x768)) #4. Send data message = ' get/http/1.1\r\n\r\n ' Print (s.sendall (message)) #5. Close Connection S.close ()

Server-side programming:

1. Open the socket;2. Bind to a specific address and port; 3. Monitor connection; 4. Establish a connection; 5. Receive/Send data

Bind Socekt: Function bind can be used to bind the socket to a specific address and port, after the binding is complete, you can turn on listening mode

Listen connection listen: Put the socket in listening mode, the function with a parameter backlog to control the number of connections, if set to 10, then there are 10 connections are waiting to be processed, at this time, the 11th request comes over, will be rejected.

Receive connection: When a client sends a connection request to the server, the server receives the connection

# _*_ Encoding:utf-8 _*_import socket#1. Create socket Object s = Socket.socket () #获取本地主机名host = Socket.gethostname () #设置端口port = 12345#2. Bind Port S.bind ((host,port)) #3. Wait for the client to connect, listen for the Socket object S.listen (5) while True:    c,addr = s.accept () #建立客户端连接    Print (' Connection address: ', addr)    c.send (' Welcome to Baidu! ')   #c. Close () #关闭连接

Python's network programming socket programming

Related Article

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.