Python Network programming

Source: Internet
Author: User

1. Python's network programming

(1), server-side code:

#!/usr/bin/python#_*_coding:utf-8_*_import socket #导入socket模块, Socket module has socket class SK = Socket.socket () Ip_port = (' 127.0.0.1 ', 9919) sk.bind (Ip_port) Sk.listen (5) while true:conn, address = Sk.accept () conn.send (' hello ') flag = T        Rue while Flag:data = Conn.recv (1024x768) print data if data = = ' exit ': Flag = False Conn.send (' SB ') Conn.close ()

(2), client code:

#!/usr/bin/python#_*_coding:utf-8_*_import socketclient = Socket.socket () Ip_port = (' 127.0.0.1 ', 9919) client.connect  (ip_port) While true:data = Client.recv (1024x768) print Data INP = Raw_input (' client: ') client.send (INP) if INP = = ' exit ': Break

(3), Operation results

Server-side:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/89/7B/wKiom1gUryyQAtHEAAAYK0jlspg274.png-wh_500x0-wm_3 -wmp_4-s_4080673152.png "title=" Qq20161029221610.png "alt=" Wkiom1guryyqatheaaayk0jlspg274.png-wh_50 "/>

Client:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/89/78/wKioL1gUr1-T2tPKAAAt2DyWz-E744.png-wh_500x0-wm_3 -wmp_4-s_2247693761.png "title=" Qq20161029221702.png "alt=" Wkiol1gur1-t2tpkaaat2dywz-e744.png-wh_50 "/>

This is a simple communication mode, but the basic can be achieved;


2. Python TCP Communication

(1), server-side code:

#!/usr/bin/python#_*_coding:utf-8_*_from socket import *from time import  ctimehost =  ' 192.168.1.112 ' port = 8810buffer = 1024addr =  (host,  Port) Ser = socket (Af_inet, sock_stream)   ser.bind (addr) Ser.listen (5)     while True:    print  ' waiting for connection ... '      cli,addr = ser.accept ()     print  ' ... connected from  :  ', addr     while true:         DATA = CLI.RECV (buffer)         print  ' Client > ', data        if data ==  ' exit ':             break         cli.send ('%s %s ' &NBsp;%  (CTime (),  data))     cli.close () Ser.close () 

(2), client code:

#!/usr/bin/python#_*_coding:utf-8_*_from Socket Import *host = ' 192.168.1.112 ' port = 8810buffer = 1024addr = (host, port) c Li = Socket (af_inet, sock_stream) cli.connect (addr) While true:data = Raw_input (' > ') if data = = ' exit ': BR Eak cli.send (data) data = CLI.RECV (buffer) print datacli.close ()

(3), Operation results

Server-side:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/89/79/wKioL1gUslXhOe3AAAApyExfQpc735.png-wh_500x0-wm_3 -wmp_4-s_4287758573.png "title=" Qq20161029222940.png "alt=" Wkiol1guslxhoe3aaaapyexfqpc735.png-wh_50 "/>

Client:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/89/7B/wKiom1gUsnOArgK6AAA45PSMcsQ349.png-wh_500x0-wm_3 -wmp_4-s_1628583907.png "title=" Qq20161029223012.png "alt=" Wkiom1gusnoargk6aaa45psmcsq349.png-wh_50 "/>


3. Python UDP communication

(1), Server side:

#!/usr/bin/python#_*_coding:utf-8_*_from Socket Import *from time import ctimehost = ' 192.168.1.112 ' port = 8812buffer = 10 24ADDR = (host, port) Ser = socket (af_inet, Sock_dgram) ser.bind (addr) print ' Waiting for message: ' While True:data, Addr = ser.recvfrom (buffer) print ' client: ', Data ser.sendto ('%s%s '% (CTime (), data), addr) Ser.close ()

(2), client

#!/usr/bin/python#_*_coding:utf-8_*_from Socket Import *host = ' 192.168.1.112 ' port = 8812buffer = 1024addr = (host, port) c Li = Socket (af_inet, sock_dgram) while true:data = Raw_input (' > ') if data = = ' exit ': Break Cli.sendto ( data, addr) data, addr = cli.recvfrom (buffer) print datacli.close ()

(3), Operation results

Server-side:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/89/79/wKioL1gUs4OQoDHAAAAkK_iQZkg979.png-wh_500x0-wm_3 -wmp_4-s_2156900548.png "title=" Qq20161029223440.png "alt=" Wkiol1gus4oqodhaaaakk_iqzkg979.png-wh_50 "/>

Client:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/89/7B/wKiom1gUs6aQpTamAAA5zvSa0Sk251.png-wh_500x0-wm_3 -wmp_4-s_2335571731.png "title=" Qq20161029223514.png "alt=" Wkiom1gus6aqptamaaa5zvsa0sk251.png-wh_50 "/>




This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1867227

Python 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.