Python's socket programming

Source: Internet
Author: User

Currently in the Learning Python stage, yesterday saw the Python socket module, respectively, TCP, UDP timestamp echo.

1. TCP Communication Server and client code

# tcpserver.py#!/usr/bin/python#-*-coding:utf-8-*-from Socket import *from time import ctimehost = ' PORT = 21156BUFSI ZE = 1024ADDR = (host,port) Tcpserversock = socket (af_inet,sock_stream) tcpserversock.bind (ADDR) Tcpserversock.listen (5 While True:print ' Waiting for connection ... ' tcpclientsock, addr = tcpserversock.accept () print ' ... connected from: ', Addrwhile True:data=tcpclientsock.recv (BUFSIZE) if not data:breaktcpClientSock.send (' [%s]%s '% (CTime (), data)) Tcpclientsock.close () Tcpserversock.close
# tcpclient.py#!/usr/bin/python#-*-coding:utf-8-*-from socket import *host = ' localhost ' PORT = 21156BUFSIZE = 1024ADDR = (host,port) Tcpclientsock = socket (af_inet,sock_stream) Tcpclientsock.connect (ADDR) while true:data = Raw_input (' > ' If not data:breaktcpClientSock.send (data) data = TCPCLIENTSOCK.RECV (BUFSIZE) if not data:breakprint Datatcpclientsock.close ()

2. UDP Communication Server and client code

 #udpServer .py#!/usr/bin/python# -*-  coding: utf-8 -*-from socket import *from time import ctimehost =   ' port = 21156bufsize = 1024addr =  (host,port) udpserversock =  Socket (Af_inet,sock_dgram) udpserversock.bind (ADDR) while true:print  ' Waiting for message ... ' Data ,addr = udpserversock.recvfrom (BUFSIZE) udpserversock.sendto (' [%s] %s '  %  ( CTime (), data),  addr) print  ' ... received from and returned to: ',addr  Tcpclientsock.close () Tcpserversock.close 
#udpClient. py#!/usr/bin/python#-*-coding:utf-8-*-from socket import *# from time import ctimehost = ' localhost ' PORT = 21156BUFSIZE = 1024ADDR = (host,port) Udpclientsock = socket (af_inet,sock_dgram) while true:data = Raw_input (' > ') if not Data:breakudpClientSock.sendto (data,addr) data,addr = Udpclientsock.recvfrom (BUFSIZE) if not data:breakprint Dataudpclientsock.close ()


Python's socket programming

Related Article

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.