Python socket programming

Source: Internet
Author: User
This article mainly for you in detail the Python network programming related information, Python socket programming, with a certain reference value, interested in small partners can refer to

Python provides two levels of access to network services.

The low-level network service supports the basic socket, which provides the standard BSD Sockets API to access all the methods of the underlying operating system socket interface.
The high-level Network Service module, Socketserver, provides a server-centric class that simplifies the development of network servers.

What is a Socket?

A socket is also called a socket, and an application usually makes a request to the network through a "socket" or responds to a network request, making it possible to communicate between hosts or between processes on a computer.

Socket () function

In Python, we use the socket () function to create a socket with the following syntax:

Socket.socket ([family[, type[, Proto]])


Family: The socket family can make Af_unix or af_inet
Type: The socket type can be divided into sock_stream or sock_dgram depending on whether it is connection-oriented or non-connected
Protocol: General does not fill the default is 0.

The socket enables simple communication between the server and the client (simulating some of SSH's functions and auto-answering)

Example of a simple program on the server side:

#!/usr/bin/env python#coding:utf-8 ' File:server.pydate:9/8/17 3:43 pmauthor:lockeyemail:lockey@123.comdesc: Socket programming server side, python3.6.2 ' import reimport socket,time,oss = Socket.socket () # Create socket Object host = ' ' #socket. Get hostname () # get local hostname port = 9999 # Set port S.bind ((host, Port)) # Bind Port S.listen (5) # Wait for client to connect while true:conn, addr = S.A. Ccept () # establishes a client connection. Print (' conneted address: '. Decode (' Utf-8 '), addr) #显示连接到服务器的客户端的地址 while true:data = Conn.recv (1024x768) #接收到的客户端的请求或者返回 if   Not data: #当返回信息为空或者请求为空时断开客户端连接 print (' Connection closed! ') Break data = Data.decode (' utf-8 ') #以下前三个条件都是设置的自动检测应答, the last one is executed according to the input content, and returns the result to the client if Re.findall (R ' who ', data): reply = ' I a M Lockey '. Encode (' Utf-8 ') elif Re.findall (R ' Gender ', data): reply = ' A boy '. Encode (' Utf-8 ') elif Re.findall (R ' age ', data   ): reply = ' all '. Encode (' Utf-8 ') else:print (' Execute com: ', data) Cmd_res = Os.popen (data). Read () If not cmd_res: #对于命令的执行如果没有返回值的话就给客户端发送一条信息 conn.send (' No response '. Encode (' UTf-8 ') Continue reply = Cmd_res.encode (' utf-8 ') conn.send (reply) #将结果发送给客户端 

A simple implementation of the client side:

#!/usr/bin/env python#coding:utf-8 ' File:client.pydate:9/8/17 3:43 pmauthor:lockeyemail:lockey@123.comdesc: Socket programming client, python3.6.2 ' import socket,time    # import Socket Module s = socket.socket ()   # Create socket Object host = ' ' #s Ocket.gethostname () # get local hostname port = 9999    # set Port good S.connect ((host, Port)) while true:cmd = input ("lockey#") #用户输入 If Len (cmd) = = 0:continue #如果用户未输入内容则继续下一次输入 s.send (Cmd.encode (' Utf-8 ')) #将用户输入的内容发送给客户端等待结果 result = S.RECV (1024x768) if not Resul T: #如果服务端没有返回结果则继续下一次输入  Continue print (Result.decode (' Utf-8 ')) #如果服务端有返回则打印结果s. Close () #用户终止程序时关闭连接

Server-side Run results

Client Run

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: 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.