File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ import socket
2
+ import sys
3
+
4
+
5
+ def client (msg ):
6
+ server_address = ('localhost' , 10000 )
7
+ sock = socket .socket (
8
+ socket .AF_INET , socket .SOCK_STREAM , socket .IPPROTO_TCP
9
+ )
10
+ print >> sys .stderr , 'connecting to {0} port {1}' .format (* server_address )
11
+ sock .connect (server_address )
12
+ response = ''
13
+ done = False
14
+ bufsize = 1024
15
+ try :
16
+ print >> sys .stderr , 'sending "{0}"' .format (msg )
17
+ sock .sendall (msg )
18
+ while not done :
19
+ chunk = sock .recv (bufsize )
20
+ if len (chunk ) < bufsize :
21
+ done = True
22
+ response += chunk
23
+ print >> sys .stderr , 'received "{0}"' .format (response )
24
+ finally :
25
+ print >> sys .stderr , 'closing socket'
26
+ sock .close ()
27
+ return response
28
+
29
+
30
+ if __name__ == '__main__' :
31
+ if len (sys .argv ) != 2 :
32
+ usg = '\n usage: python echo_client.py "this is my message"\n '
33
+ print >> sys .stderr , usg
34
+ sys .exit (1 )
35
+
36
+ msg = sys .argv [1 ]
37
+ client (msg )
You can’t perform that action at this time.
0 commit comments