How to run grpc on ipv4 only

2024/11/15 1:05:57

I'm going to run a grpc server on IPv4 address like this:

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
protoc_pb2_grpc.add_ProtocServicer_to_server(StockProtocServicer(), server)
server.add_insecure_port('0.0.0.0:5000')
server.start()

But it it's listening to IPv6. Here is the output for lsof -i -n -P | grep 5000:

python    7302             dev    6u  IPv6 224100      0t0  TCP *:5000 (LISTEN)

I've tested listening on IPv6 to see what happens, like this:

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
protoc_pb2_grpc.add_ProtocServicer_to_server(UserProtocServicer(), server)
server.add_insecure_port('[::]:5000')
server.start()

but the result was the same:

python    7366             dev    6u  IPv6 225782      0t0  TCP *:5000 (LISTEN)

I also tried to use the actual IP address instead of 0.0.0.0:

python    7392             dev    6u  IPv6 226111      0t0  TCP 192.168.1.23:5000 (LISTEN)

Although I can use the server's IPv4 address on my client, I want to run the server only on IPv4. How can I do that?

Answer

TLDR: If you use an IPv4 address, it's using IPv4, not IPv6. You have what you want, even though lsof says IPv6.


lsof and "IPv4-mapped IPv6 address" explanation

When using the IPv4 address, my python client still shows type as IPv6 when lsof -nP -i4TCP | grep appname.

From man lsof:

When an open IPv4 network file's address is mapped in an IPv6 address, the open file's type will be IPv6, not IPv4, and its display will be selected by '6', not '4'.

This means our applications uses the IPv6 network file/socket, but it uses the IPv4 protocol. Apparently, this is convenient for applications (e.g. python, gRPC) to handle - they don't have to worry about IPv4 and IPv6, they just support IPv6 and it will map to IPv4 if needed.

For more information about mapped addresses, see

  • Purpose of IPv4 mapped IPv6 address.
  • IPv4-mapped IPv6 addresses

Bonus question:

What if you only have the domain name (e.g. google.com) and want to use IPv4, not IPv6, even though the IPv6 DNS record (AAAA) exists: dig AAAA google.com).

Answer: To do this, you could use dig A example.com and use that the server address. I am looking for a better way to do this though.

https://en.xdnf.cn/q/71890.html

Related Q&A

Python/PyCharm mark unused import as used

I need to import a resource_rc.py file in my module. It is immediately marked by PyCharm as "unused". Is there a way to mark "unused" imports and also variables, etc. as used in Pyt…

Replacing every 2nd element in the list

I got a 2 dimensional list:[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]]In which I need to change every second element to 0, starting from first one. So it s…

Are C++-style internal typedefs possible in Cython?

In C++ its possible to declare type aliases that are members of a class or struct:struct Foo {// internal type aliastypedef int DataType;// ... };Is there any way to do the same thing in Cython? Ive t…

How do I use a regular expression to match a name?

I am a newbie in Python. I want to write a regular expression for some name checking. My input string can contain a-z, A-Z, 0-9, and _ , but it should start with either a-z or A-Z (not 0-9 and _ ). I…

python - multiprocessing module

Heres what I am trying to accomplish - I have about a million files which I need to parse & append the parsed content to a single file. Since a single process takes ages, this option is out. Not us…

How to make VSCode always run main.py

I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configurat…

Why does tesseract fail to read text off this simple image?

I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.Here is the image:I have tried scaling it, grayscaling it, and adjust…

python click subcommand unified error handling

In the case where there are command groups and every sub-command may raise exceptions, how can I handle them all together in one place?Given the example below:import click@click.group() def cli():pass…

Data structure for large ranges of consecutive integers?

Suppose you have a large range of consecutive integers in memory, each of which belongs to exactly one category. Two operations must be O(log n): moving a range from one category to another, and findin…

polars slower than numpy?

I was thinking about using polars in place of numpy in a parsing problem where I turn a structured text file into a character table and operate on different columns. However, it seems that polars is ab…