How to implement socket communication in python

Source: Internet
Author: User
Tags php website
This article mainly introduces python's method for implementing simple socket communication, and analyzes the specific implementation skills of the socket communication server and client in the form of instances, for more information about how to implement simple socket communication in python, see the example in this article. We will share this with you for your reference. The details are as follows:

I started to access python and implemented a helloworld program-socket communication demo about udp.

First, the implementation on the server side is as follows:

Import socket, tracebackhost = ''# Bind to all interfaces port = 51500 # Step1: create socket object s = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) # Step 2: Set the socket option (optional) s. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1) # Step3: bind to a port s. bind (host, port) # Step4: Listen to the connection on this port while 1: try: message, address = s. recvfrom (8192) print "Got data from", address s. sendto ("Data is already ed succeefully. ", address) cipher T (KeyboardInterrupt, SystemExit): print" raise "raise cipher T: print" traceback "traceback. print_exc ()

If the host is left blank, it means it can be bound to all interfaces and addresses. no matter which client requests are sent, as long as they are bound to the same port, then the server can listen to this request.

In a tcp connection, the listen or accept function is required to listen to client requests, and a dedicated socket and remote connection are required.

Next, let's implement our client:

Import socket, sys # Step1: input host and port information host = raw_input ('Please input host name: ') textport = raw_input ('Please input textport:') # Step2: create socket object s = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) try: port = int (textport) handle T ValueError: port = socket. getservbyname (textport, 'udp') # Step 3: Open the socket connection s. connect (host, port) # Step4: Send data print "Enter data to transmit:" data = sys. stdin. readline (). strip () s. sendall (data) # Step 5: receive data from the server print "Looking for replies; press Ctrl-C or Ctrl-Break to stop" while 1: buf = s. recv (2048) if not len (buf): break sys. stdout. write (buf)

This example is as simple as the C language version. It seems that as long as you understand socket programming, it will be almost the same to implement it in different languages.

For more information about how to implement socket communication in python, refer to the Chinese PHP website!

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.