Python Socket implements simple TCP Server/client function example, pythonclient

Source: Internet
Author: User

Python Socket implements simple TCP Server/client function example, pythonclient

This example describes how to implement simple TCP Server/client functions using Python Socket. We will share this with you for your reference. The details are as follows:

There are countless articles on sockets on the Internet. Record your learning details. For future review and use.

The translation of socket in Chinese is socket, and the general sense is unsatisfactory. A simple understanding is a management unit formed by ip + port. It is also the interface called by the application in the program.

Here we will first introduce howStart tcp server.

In the server section of the tcp connection, start an ip address and port and listen to the port. When receiving a request from the client, use a new port port2 to establish a connection with the client.

The process of starting a socket listener is as follows:

Create socket
Bind Port
Start listening
Establish connection + continue listening

Code:

'''This is a testing programthe program is used to start server'''import socketimport sysdef start_tcp_server(ip, port):  #create socket  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  server_address = (ip, port)  #bind port  print 'starting listen on ip %s, port %s'%server_address  sock.bind(server_address)  #starting listening, allow only one connection  try:    sock.listen(1)  except socket.error, e:    print "fail to listen on port %s"%e    sys.exit(1)  while True:    print "waiting for connection"    client,addr = sock.accept()    print 'having a connection'    client.close()if __name__ == '__main__':  start_tcp_server('10.20.0.20', 12345)

Here is a common technique. In start_tcp_server, we usually use the ip address of the Local Machine. for the universality of the program, it is best to use the method of calling the function to obtain the ip address.

Two functions are used.socket.gethostnameAndsocket.gethostbyname('name')

ip = socket.gethostbyname(socket.gethostname())

However, the problem is that the IP address is 127.0.0.1.

For ip addresses obtained through configuration or dhcp, refer to related articles on this site.

Socket client initiates a connection

The process is as follows:

Create Interface
Initiate a connection
The interface creation parameters are the same as those of socket server.
The connection initiation function is socket. connect (ip, port)

The ip address and port in this region are the ip address of the socket server and the listening port.

Sample Code:

# -*- coding: utf-8 -*-'''This is a testing programthe program is used to test socket client'''import socketimport sysdef start_tcp_client(ip, port):  #server port and ip  server_ip = ip  servr_port = port  tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  try:    tcp_client.connect((server_ip, server_port))  except socket.error:    print 'fail to setup socket connection'  tcp_client.close()

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.