Python Learning Network programming

Source: Internet
Author: User

First, the Socket module     socket module provides the bottom interface, through the socket module can realize different computer communication     ip+ Port locates the specific location of the application in the computer, socket binding Ip+port to establish the link, the socket method details:     socket (family, type,  Proto):  socket function creates a socket object, creates a connection using the socket object,     family protocol family, default Af_inet,ipv4 protocol, type, Socket type, proto protocol type         bind ((ip, port)):  Bind server address      listen (n):  monitors the link created by the socket,  n represents the number of connection queues specified,     connect (' 127.0.0.1 ',  8080)):  connected to server Side     accept (): Waiting for client connections, returning a tuple of connection objects and client IPs      Recv (n):  receives data, n means to accept fixed data     send:  send data           Example: Analog client server-side communication     server:    #!/usr/bin/python     # --*-- coding: utf-8 --*--         import socket    phone = socket.socket (Socket.af_inet, socket. Sock_stream)     phone.bind ((' 127.0.0.1 ',  8080)          phone.listen (5)         print (' starting ... ')          conn, addr = phone.accept ()     print ( conn)     print (' client addr:  ',  addr)     print (' Ready  to read msg ')     client_msg = conn.recv (1024x768)      print (' client msg: %s '  % (client_msg))     conn.send (Client_msg.upper () )     conn.close ()     phone.close ()          client    #!/usr/bin/pytthon    # --*-- coding:  utf-8 --*--    import socket&Nbsp;   phone = socket.socket (Socket.af_inet, socket. Sock_stream)     phone.connect ((' 127.0.0.1 ',  8080)          phone.send (' Hello '. Encode (' Utf-8 '))         back_msg  = PHONE.RECV (1024x768)     print (Back_msg.decode (' GBK '))      Phone.close ()          analog loop send receive data     server     import socket    phone = socket.socket (Socket.AF_INET,  socket. Sock_stream)     phone.bind ((' 127.0.0.1 ',  8080))     phone.listen (5 )         while True:         print (' starting ... ')         conn, addr =  Phone.accept ()       &Nbsp; print (' client addr  ',  addr)         while  True:            try:                 client_msg = conn.recv (1024x768)                 if  not client_msg: break                 print (' client_msg: %s '  %client_msg)                  conn.send (Client_msg.upper ())              except Exception:                 break         conn.close ()      phone.close ()         client    import  Socket    phone = socket.socket (Socket.af_inet, socket. Sock_stream)     phone.connect ((' 127.0.0.1 ',  8080)          while true:        msg = input ("> >:  ")         if not msg: continue         phone.send (Msg.encode (' Utf-8 '))          BACK_MSG = PHONE.RECV (1024x768)         print (back_ Msg.decode (' Utf-8 '))     phone.close ()          Simulate remote execution of commands and return results     server    import socket     import subprocess    phone&Nbsp;= socket.socket (Socket.af_inet, socket. Sock_stream)     phone.bind ((' 127.0.0.1 ',  8080))     phone.listen (5 )         while True:         print (' starting ... ')         conn,addr = phone.accept ()         print (' client addr:  ',  addr)          while True:             try:                 CMD = CONN.RECV (1024x768)                  if not cmd: break                 res = subprocess. Popen (cmd.Decode (' Utf-8 '),  shell=true, stdout=subprocess. Pipe, stderr=subprocess. PIPE)                 err  = res.stderr.read ()                  if err:                     cmd_res = err                 else:                     cmd_res = res.stdout.read ()                  conn.send (Cmd_res)             except Exception:           &nbSp;     break        conn.close ()      phone.close ()         client    import  socket    phone = socket.socket (Socket.af_inet, socket. Sock_stream)     phone.connect ((' 127.0.0.1 ',  8080)          while true:        cmd = input (' > >:  '). Strip ()         if not cmd: continue         phone.send (Cmd.encode (' Utf-8 '))          BACK_MSG = PHONE.RECV (1024x768)         print (back_ Msg.decode (' GBK '))     phone.close ()          sticky pack phenomenon, The packet is disconnected irregularly and there is an unwanted disconnection phenomenon     server    from socket import *    import  Time    tcp_server = socket (Af_inet, sock_stream)      Tcp_server.bind ((' 127.0.0.1 ',  8080))     tcp_server.listen (5)          conn, addr = tcp_server.accept ()     res1 =  CONN.RECV (5)     RES2 = CONN.RECV (5)     res3 =  CONN.RECV (7)     print (Res1.decode (' GBK '))     print (Res2.decode (' GBK ')     print (Res3.decode (' GBK '))         client     from socket import *    import time         tcp_client = socket (Af_inet, sock_stream)      tcp_client.connecT ((' 127.0.0.1 ',  8080))         tcp_client.send (' Hello world '). Encode (' Utf-8 '))     time.sleep (5)     tcp_client.send (' egon666 '. Encode ( ' Utf-8 '))


This article is from the "Linux Technology" blog, so be sure to keep this source http://haoyonghui.blog.51cto.com/4278020/1939996

Python Learning Network programming

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.