Skip to content

Programming with gRPC in Python and building services.

Notifications You must be signed in to change notification settings

Brisinger/gRPC_Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

gRPC in Python

This repository is based on my understanding from the LinkedIn Learning course gRPC in Python. The full course is available from LinkedIn Learning .

gRPC in Python

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.

Installing

  1. To use these exercise files, you must have the following installed:

    • Python (from version 3.8 and up)
    • The protoc compiler
    • Install [protoc] in Mac OS using Homebrew with command "brew install protobuf" in Mac Terminal.
  2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree.

  3. 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.
  4. 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".
  5. 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 - Google's data interchange format

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.

Protocol Buffers Python

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/

Code generator

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:
    1. 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.
    2. A python module with name "foo_pb2.py" is generated by protoc.
    3. 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

Resolving connection failure to target host while running gRPC server

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.
  1. 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.
  2. 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.
  3. Run the gRPC server to prevent the connection from getting refused while client dials the gRPC server.