~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to Exercises_python/Solutions/socket_echo_client.py

  • Committer: Dave Kuhlman
  • Date: 2015-10-03 18:25:17 UTC
  • mfrom: (35.1.5 Materials)
  • Revision ID: dkuhlman@davekuhlman.org-20151003182517-b2m7eltjdapw46zh
Review for Cisco.  Merged checkpoint 10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
Synopsis:
 
5
    Implement a client that uses a low level Python socket to echo a response.
 
6
    Use the "socket" module in the Python standard library.
 
7
Usage:
 
8
    python socket_echo_client.py
 
9
"""
 
10
 
 
11
import socket
 
12
 
 
13
 
 
14
def request():
 
15
    HOST = ''
 
16
    PORT = 50007              # The same port as used by the server
 
17
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
18
    sock.connect((HOST, PORT))
 
19
    msg = 'Hello, world'
 
20
    sock.sendall(msg)
 
21
    print 'Sent', repr(msg)
 
22
    data = sock.recv(1024)
 
23
    sock.close()
 
24
    print 'Received', repr(data)
 
25
 
 
26
 
 
27
def main():
 
28
    request()
 
29
 
 
30
 
 
31
if __name__ == '__main__':
 
32
    main()