Python socket programming half-duplex chat

Source: Internet
Author: User

Server:

#! /Usr/bin/pythonimport socketfrom time import ctimeimport sysbufsize = 1024 host = '2017. 0.0.1 'port = 8100 address = (host, Port) server_sock = socket. socket (socket. af_inet, socket. sock_stream) server_sock.bind (Address) server_sock.listen (1) While true: Print 'Waiting for connection... 'clientsock, ADDR = server_sock.accept () print 'stored ed from: ', addrwhile true: Data = clientsock. recv (bufsize) print '% s \ n % s' % (ctime (), data) Data = raw_input (">") clientsock. send (data) clientsock. close () server_sock.close ()

Client:

#! /Usr/bin/pythonfrom socket import * From time import ctimeimport sysbufsize = 1024 host = sys. argv [1] Port = int (sys. argv [2]) ADDR = (host, Port) client_sock = socket (af_inet, sock_stream) client_sock.connect (ADDR) While true: Data = raw_input (">") if not data: breakelse: client_sock.send (data) Data = client_sock.recv (bufsize) print '% s \ n % s' % (ctime (), data) client_sock.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.