0

First time asking a question here, but I'll try my best to include everything needed.

Working on a personal project to create a Discord bot that can play .wav and .mp3 files from a personal FTP server I have set up. I can access the server and display the directory using ftplib, and seem to be able to attempt to retrieve a binary, but this is where I'm stuck. It's my understanding that .mp3 is stored as a bytes object, and I'm not sure how to go about retrieving that. Also, using pydub to play audio for testing purposes. Will learn how to play audio through a discord bot in the future.

ftp_server = ftplib.FTP()
ftp_server.connect('10.0.0.76', 21)
ftp_server.login('music','test')


# force UTF-8 encoding
ftp_server.encoding = "utf-8"
ftp_server.dir()
# Enter File Name with Extension
filename = 'test.mp3'

with open(filename, "rb") as file:
    data = ftp_server.retrbinary(f"RETR {filename}", file.read())

file.close()
song = AudioSegment.from_file(io.BytesIO(data), format="mp3")
play(song)

# Close the Connection
ftp_server.quit()

This results in the following error (after dir):

drwxrwxrwx 1 ftp ftp               0 Aug 21 08:39 cert
-rw-rw-rw- 1 ftp ftp              46 Aug 21 07:39 desktop.ini
-rw-rw-rw- 1 ftp ftp        20243017 Aug 18 02:55 Gorillaz - Humanz - Side A.mp3
drwxrwxrwx 1 ftp ftp               0 Aug 22 00:17 Music
drwxrwxrwx 1 ftp ftp               0 Aug 22 08:11 See Without Eyes - Glitchmob
-rw-rw-rw- 1 ftp ftp          261847 Sep 13  2012 test.mp3
-rw-rw-rw- 1 ftp ftp      1784873150 Aug 18 08:29 TOOL Fear Inoculum.wav
Traceback (most recent call last):
  File "C:/Users/Wrex/PycharmProjects/ForsakenWorldRadio/main.py", line 42, in <module>
    data = ftp_server.retrbinary(f"RETR {filename}", file.read())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 437, in retrbinary
    callback(data)
TypeError: 'bytes' object is not callable

My ask: Am I going about retrieving an .mp3 file the right way? Is there a better way to play audio from an FTP server in discord, different libraries, documentation, etc. So far most of my searches have been fruitless.

Thank you for taking the time to look at this! Let me know if I need to add more information.

  • Welcome to Stack Overflow. ```retrbinary``` retrieves a file. Why are you opening a file and using ```file.read()```? ```retrbinary``` also expects a callback when it grabs data from the ftp server. – ewokx Aug 23 '22 at 00:00
  • it certainly helped me to better understand what is going on with retrbinary, so thank you for the answer! It seems now that I am running into a permissions error. – Axiou_Lepida Aug 23 '22 at 00:55

0 Answers0