Srevilo
Srevilo

Reputation: 174

Python get data from named pipe

How do I read from a named pipe in Python 3.5.3?

The pipe's name and location is /tmp/shairport-sync-metadata and it can be viewed by anybody, but only modified by the shairport-sync user. Other websites and questions say to use os.mkfifo("pipe-location") but I've been getting this error:

>>> import os
>>> os.mkfifo("/tmp/shairport-sync-metadata")

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    os.mkfifo("/tmp/shairport-sync-metadata")
FileExistsError: [Errno 17] File exists

Is there a way to get around this? Sorry for the nooby question.

Upvotes: 2

Views: 2373

Answers (1)

falsetru
falsetru

Reputation: 369424

os.mkfifo is used to create fifo. Use open to open/read fifo already exist:

with open('/tmp/shairport-sync-metadata') as f:   # add `rb` for binary mode
    # line-by-line read
    for line in f:
        print(line)

    # f.read(1024)  # to read 1024 characters

Upvotes: 2

Related Questions