This repository is based on my understanding from the LinkedIn Learning course gRPC in Python. The full course is available from LinkedIn Learning .
gRPC allows clients and server applications to communicate more efficiently on a connected system. Knowing how to use gRPC can come in handy if you’re programming in a language like Python. In this Repo designed uniquely for Python developers, based on the course by instructor Miki Tebeka, the essentials of gRPC is covered in a lucid hands-on manner to get started with this easy-to-use tool.
Get a brief introduction to gRPC and the working parts of its ecosystem: protocol buffers, HTTP/2-based transport, .proto files, enumerations, nested types, and JSON encoding. Learn how to run the gRPC server, create a gRPC client, define a streaming end point to send data to the client, and handle streaming responses from the server. Along the way, we will be using tools like interceptors and gRPC Gateway.
-
To use these exercise files, you must have the following installed:
-
Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree.
-
Create a virtual environment based on your root directory name using pyenv, for Mac users.
- In mac OS The basic premise of all Python development is to never use the system Python.
- You do not want the Mac OS X 'default Python' to be 'python3.'
- You want to never care about default Python. To stop caring about the default python use pyenv.
- Install in mac pyenv using homebrew using the command "brew install pyenv".
- This will install pyenv in user home folder.
-
Windowws users can use virtualenv library.
- The package venv comes with Python 3.
- Python 2 does not contain venv.
- The library virtualenv offers more functionality than venv.
- View the following link for a list of features venv does not offer compared to virtualenv.
- To upgrade pip before installing virtualenv "python3 -m pip install --upgrade pip".
- Install virtualenv using pip3, "pip3 install virtualenv".
-
You'll also need to install the external Python dependencies for instance protobuf runtime, see requirements.txt. we'll be using grpcurl, you can install it via your package manager (e.g.
brew
,apt
,choco
...) or from the release section
Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. You can learn more about it in protobuf's documentation.
In most cases you should install the library using pip or another package manager:$ pip install protobuf The packages released on https://pypi.org/project/protobuf/#files include both a source distribution and binary wheels.For user documentation about how to use Protobuf Python, see https://protobuf.dev/getting-started/pythontutorial/
The code for the Protobuf Python code generator lives in //src/google/protobuf/compiler/python. The code generator can output two different files for each proto foo.proto:
- foo_pb2.py: The module you import to actually use the protos.
- foo_pb2.pyi: A stub file that describes the interface of the protos.
- The foo_pb2.pyi file is useful for IDEs or for users who want to read the output file. The foo_pb2.py file is optimized for fast loading and is not readable at all.
- To generate a module with a static descriptor of each message type in your .proto file e.g. foo.proto:
- Run the command in the terminal with the directory containing the .proto file as the current working directory: protoc --python_out=. foo.proto. The dot(.) designates current working path.
- A python module with name "foo_pb2.py" is generated by protoc.
- Use this module generated by protoc(compiler) with a metaclass to create necessary Python data access class at runtime.
Note that the pyi file is only generated if you pass the pyi_out option to protoc:$ protoc --python_out=pyi_out:output_dir
Check if current port is open from host in terminal by running the command "nc -zv host port".
- The flag -z ensures that nc (a.k.a netcat) doesn't send data after establishing a connection.
- The flag -v enables verbose out. If the connection is established one should see a message.
- The "host" and "port" is replaced with doman address and port no. respectively. Also check the Firewal settings blocking the connection or Network configuration of your system.
- Firewall settings:
- Open "System Preferences" from the Apple menu or the Dock.
- Click on "Security & Privacy" or "Security."
- Go to the "Firewall" tab.
- If the firewall is enabled, click on the lock icon and enter your administrator password to make changes.
- Review the list of applications and services in the "Allow incoming connections" section. Ensure that the application or service you are trying to connect to is allowed to receive incoming connections. If it's not listed, you can click on the "+" button to add it.
- If necessary, you can also click on the "Advanced..." button to further customize firewall settings.
- Network configurations:
- Open "System Preferences" from the Apple menu or the Dock.
- Click on "Network."
- Select the active network connection (Wi-Fi, Ethernet, etc.) from the left-hand side.
- Click on the "Advanced..." button.
- Go to the "Firewall" or "Proxies" tab, depending on your macOS version.
- Review the firewall and proxy settings to ensure they are not blocking the connection you're attempting. If necessary, adjust the settings accordingly.
- Run the gRPC server to prevent the connection from getting refused while client dials the gRPC server.