-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.py
43 lines (34 loc) · 1.5 KB
/
tcp_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
######################################################################
######################################################################
### ____ _ ###
## / ___|| |_ ___ _____ _ __ ___ __ _ ___ ##
## \___ \| | | | \ \ / / _ \| '_ \ / __/ _` / __| ##
## ___) | | |_| |\ V / (_) | | | | (_| (_| \__ \ ##
## |____/|_|\__, | \_/ \___/|_| |_|\___\__,_|___/ ##
## |___/ ##
### ###
######################################################################
######################################################################
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
try:
# Send data
message = b'This is the message. It will be repeated.'
print('sending {!r}'.format(message))
sock.sendall(message)
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(4096)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
sock.close()