Python network programming Python socket programming, python Network Programming

Source: Internet
Author: User

Python network programming Python socket programming, python Network Programming

Python provides two levels of network services for access.

Low-level network services support basic Sockets. It provides the standard BSD Sockets API and can access all the methods of Socket interfaces of the underlying operating system.
SocketServer, a high-level network service module, provides a server center class to simplify the development of network servers.

What is Socket?

A Socket is also called a "Socket". An application sends a request to or responds to a network request through a "Socket" to enable inter-host or computer processes to communicate.

Socket () function

In Python, we use the socket () function to create a socket. The syntax format is as follows:

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

Parameters

Family: the socket family can make AF_UNIX or AF_INET
Type: the socket type can be divided into SOCK_STREAM or SOCK_DGRAM based on whether it is connection-oriented or not.
Protocol: Generally, the default value is 0.

Socket implements simple communication between the server and the client (simulating some of the ssh functions and automatic response)

Simple program example on the server:

#! /Usr/bin/env python # coding: UTF-8 ''' file: server. pydate: 9/8/17 PMauthor: lockeyemail: lockey@123.comdesc: socket programming server side, python3.6.2 ''' import reimport socket, time, oss = socket. socket () # create a socket object host = '2017. 0.0.1 '# socket. gethostname () # obtain the local host name port = 9999 # Set the port s. bind (host, port) # bind port s. listen (5) # Wait for the client to connect while True: conn, addr = s. accept () # create a client connection. Print ('connectedaddress :'. decode ('utf-8'), addr) # display the address of the client connected to the server while True: data = conn. recv (1024) # if not data is returned for the received client request or if not data: # Disconnect the client from print ('Connection closed! ') Break; data = data. decode ('utf-8') # The first three conditions below are all set automatic detection responses, and the last one is the command execution based on the input content, return the result to the client if re. findall (r 'who ', data): reply =' I am lockey '. encode ('utf-8') elif re. findall (r 'gender', data): reply = 'A '. encode ('utf-8') elif re. findall (r 'age', data): reply = '23 '. encode ('utf-8') else: print ('execute com: ', data) pai_res = OS. popen (data ). read () if not passed _res: # if no return value is returned for command execution, a message conn is sent to the client. send ('no response '. encode ('utf-8') continue reply = pai_res.encode ('utf-8') conn. send (reply) # send the result to the client

Simple client implementation:

#! /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 a socket object host = '2017. 0.0.1 '# socket. gethostname () # Get the local host name port = 9999 # Set the port s. connect (host, port) while True: cmd = input ("lockey #") # user input if len (cmd) = 0: continue # If the user does not enter the content, continue the next input s. send (cmd. encode ('utf-8') # Send the content entered by the user to the client and wait for result = s. recv (1024) if not result: # if the server does not return the result, continue to input continue print (result. decode ('utf-8') # print the result s if the server returns the result. close () # close the connection when the user terminates the program

Server running result

Client running

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.