January 2019
Beginner to intermediate
776 pages
19h 58m
English
In Python, passing command-line arguments to a script and parsing them in the script can be done using the argparse module. This is available in Python 2.7. For earlier versions of Python, this module is available separately in Python Package Index (PyPI). You can install this via easy_install or pip.
In this recipe, three arguments are set up—a hostname, port number, and filename. The usage of this script is as follows:
$ python 11_7_socket_errors.py --host=<HOST> --port=<PORT> --file=<FILE>
In the preceding recipe, msg.encode('utf-8') encodes the message into UTF-8, and buf.decode('utf-8') decodes the received UTF-8 format.
If you try the preceding recipe with a non-existent host, this script will print an address ...
Read now
Unlock full access