forked from QPG-MIT/hwserver-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
38 lines (28 loc) · 957 Bytes
/
client.py
File metadata and controls
38 lines (28 loc) · 957 Bytes
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
import socket, sys, urllib
message = sys.argv[1]
DEFAULT_IP = 'localhost'
DEFAULT_PORT = 36577
def recv(connection,delim='\n',recv_buffer=4096):
buffer = ''
while True:
data = connection.recv(recv_buffer)
assert data, 'Client disconnected while receiving.'
buffer += data
if data[-1] == '\n':
return buffer[0:-len(delim)] # Remove delim
# 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 = (DEFAULT_IP, DEFAULT_PORT)
print 'connecting to %s port %s' % server_address
sock.connect(server_address)
try:
# Send data
print 'sending "%s"' % message
sock.sendall(urllib.quote_plus(message.strip())+'\n')
# Look for the response
resp = urllib.unqoute_plus(recv(sock))
print 'received "%s"' % resp.strip()
finally:
print 'closing socket'
sock.close()