Python Socket Socket Programming

Source: Internet
Author: User

#!/usr/bin/python #client from socket import * HOST = ' 127.0.0.1 ' PORT = 21567 Bufsiz = 1024x768 ADDR = (host,port) tcpclis Ock = socket (af_inet, Sock_stream) tcpclisock.connect (ADDR) recv_s = Tcpclisock.recv (bufsiz) print recv_s try:while True:send_c = Raw_input (' clinet: ') tcpclisock.send (' Client:%s '% (send_c)) recv_s = Tcpclisock. Recv (bufsiz) print recv_s finally:tcpCliSock.close ()

Server:

#!/usr/bin/python  from socket import *  host =  '  PORT  = 21567 BUFSIZ = 1024 ADDR =  (Host, port)  tcpsersock =  socket (Af_inet, sock_stream)  tcpsersock.bind (ADDR)  tcpsersock.listen (1)   try:      while true:         print   ' waiting for connection ... '          tcpclisock,  addr = tcpsersock.accept ()          print  ' ... Connected from: ',  addr         tcpclisock.send (' Please have a talk ')          try:              while True:                  RECV_C = TCPCLISOCK.RECV (BUFSIZ)                   print recv_c                  send_s =  Raw_input (' server:  ')                   tcpclisock.send (' server: %s '  %  (send_s))           finally:              Tcpclisock.close ()  finally:     tcpsersock.close ()



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