python(mutlti conn socket client)

import socket
import sys

messages = [ b'This is the message. ',
 b'It will be sent ',
 b'in parts.',
 ]
server_address = ('192.168.16.130', 9998)

# Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM) for i in range(11000)]
print(socks)
# Connect the socket to the port where the server is listening
print('connecting to %s port %s' % server_address)
for s in socks:
 s.connect(server_address)

for message in messages:

# Send messages on both sockets
 for s in socks:
 print('%s: sending "%s"' % (s.getsockname(), message) )
 s.send(message)

# Read responses on both sockets
 for s in socks:
 data = s.recv(1024)
 print( '%s: received "%s"' % (s.getsockname(), data) )
 if not data:
 print( 'closing socket', s.getsockname() )
import socket

HOST = 'localhost'  # The remote host
PORT = 9998  # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
    msg = bytes(input(">>:"), encoding="utf8")
    s.sendall(msg)
    data = s.recv(1024)

    #
    print('Received', data)
s.close()

发表评论