Multi-process concurrency simple socket

Source: Internet
Author: User

Server---------------------
This simple implementation: The disadvantage of the client to start a process, the process is more than the machine to jump
#!/usr/bin/env python
# Encoding:utf-8
# DATE:2018/6/17


From socket Import *
From multiprocessing import Process


# Responsible for communication
DEF talk (conn):
While True:
Try
data = CONN.RECV (1024)
If not data:break
Conn.send (Data.upper ())
Except Connectionreseterror:
Break
Conn.close ()


# Build Links
def server (IP, port):
Server = socket (af_inet, SOCK_STREAM)
Server.setsockopt (Sol_socket, SO_REUSEADDR, 1)
Server.bind ((IP, port))
Server.listen (5)

While True:
conn, addr = Server.accept ()
p = Process (Target=talk, args= (conn,))
P.start ()
Server.close ()


if __name__ = = ' __main__ ':
Server (' 127.0.0.1 ', 8080)



Client-------------------
#!/usr/bin/env python
# Encoding:utf-8
# DATE:2018/6/17

# clients can start multiple processes and start one is a process
From socket Import *

Client = socket (Af_inet, SOCK_STREAM)
Client.connect (' 127.0.0.1 ', 8080)

While True:
msg = input (' >>>: '). Strip ()
If not msg:continue
Client.send (Msg.encode (' Utf-8 '))
data = CLIENT.RECV (1024)
Print (Data.decode (' Utf-8 '))





Multi-process concurrency simple socket

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.