Python socket network programming TCP/IP server and client communication, pythonsocket

Source: Internet
Author: User

Python socket network programming TCP/IP server and client communication, pythonsocket

Python socket network programming

I have bought two books, "python programming from getting started to practice" and "python core programming 3". the first book mainly talks about some basic syntaxes and some basic usage methods, while the second book goes a lot deeper. In my opinion, it is also a little bit of knowledge. I just saw this part of network programming, there are still a lot of things I don't quite understand, but I want to keep searching for materials and learning through my own constant exploration. It should become transparent sooner or later .......

This part mainly uses the socket module. In this module, you can find the socket () function, which is used to create a socket object. The socket also has its own method set, these methods can implement socket-based network communication.

Socket () module functions

To create a socket, you must use the socket. socket () function. Its general syntax is as follows:

socket(socket_family, socket_type, protocol=0)

Socket_family is AF_UNIX or AF_INET, and socket_type is SOCK_STREAM or SOCK_DGRAM. protocol is usually omitted. The default value is 0.

Therefore, to create a TCP/IP socket, you can use the following method to call socket. socket ().

tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Create a UDP/IP socket and call the following method:

udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

Create a TCP Server

The process for creating a TCP server is basically as follows, not the actual code

Ss = socket () # create a server socket ss. bind () # bind the socket to the address to ss. listen () # Listening to inf_loop: # server infinite loop cs = ss. accepr () # accept client connection comm_loop: # communication loop cs. recv ()/cs. send () # receive/send a dialog) cs. close () # close the client socket ss. close () # disable server socket (optional)

As mentioned above, the basic process for creating a TCP server may be slightly different, but the basic idea should be the same. below is the code for truly wearing a server:

#! /Usr/bin/env python #-*-coding: UTF-8-*-from socket import * # introduce the socket attribute to the namespace HOST = ''# This is for bind () the method ID indicates that any available address PORT = 21571 # PORT number BUFSIZ = 1024 # buffer size, 1 kbADDR = (HOST, PORT) # address ???? TcpSerSocket = socket (AF_INET, SOCK_STREAM) # create a tcp socket tcpSerSocket. bind (ADDR) # bind the address to the socket tcpSerSocket. listen (5) # Set and start socket listening while True: # infinite loop, waiting for the client to connect print ('Waiting for connection... ') tcpCliSocket, addr = tcpSerSocket. accept () # passively accept client connection print ('... connected from: ', addr) while True: # dialog loop, waiting for the client to send the message data = tcpCliSocket. recv (BUFSIZ) # receive client message if not data: # if the message is blank, jump out of the dialog loop and close the current connection break tcpCliSocket. send (data) # If a message is received, the message will not be sent back to the client tcpCliSocket. close () tcpSerSocket. close ()

Create a TCP client

Same as above, a simple non-code process

Cs = socket () # create a client socket cs. connect () # Try to connect to the server comm_loop: # communication loop cs. send ()/cs. recv () # Send/receive cs. close () # close the client socket

Creating a client in practice is also a step for translation.

#! /Usr/bin/env python #-*-coding: UTF-8-*-from socket import * HOST = 'localhost' # server HOST name PORT = 21571 # PORT number BUFSIZ = 1024 # buffer ADDR = (HOST, PORT) # address tcpCliSocket = socket (AF_INET, SOCK_STREAM) # create a client socket tcpCliSocket. connect (ADDR) # connect to the server while True: # communication Loop data = input ('>') # client input information if not data: # if the input information is empty, the loop is displayed, disable communication break data = str. encode (data) tcpCliSocket. send (data) # send client information data = tcpCliSocket. recv (BUFSIZ) # accept the server's returned information if not data: # if the server does not return information, disable the communication loop break print ('get: ', data. decode ('utf-8') tcpCliSocket. close ()

Of course, this is only the most basic communication, and the host name, port number and other things are not very familiar at the moment, what is currently done is to communicate on the same computer, the port number also needs to be consistent, what should I do if different computers communicate? I'm just a little white .....

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.