Python Pyaudio -- How to play a file streamed via HTTP - python

I am trying to figure out how to play an mp3 that exists on my server served through HTTP. I tried to figure out pyglet but there were too many issues with AVBin to make that work (something about dividing by zero in the source code). So, I decided to try PyAudio, but I can't figure out how to stream an mp3 source from HTTP with it. All the examples are wav files and I need examples rather than the docs or I'm afraid I'll have to figure out the particulars of how audio works on the lowest level. All the documentation is confusing for those of us that don't know the lingo (sample rate, etc). Can anyone help?

What is your source and target server environments? [ windows / linux / mac? ]. Also are your trying to play an audio file or video?
In either case you can use Gstreamer with python. I've windows laptop, so I tried with Gstreamer 1.0.x from here
Online examples are for Gstreamer 0.10 that does not work with newer Gstreamer 1.0. There lots of modifications that are platform specific.
However, on windows Gstreamer comes with gst-launch binary which works off the shelf. [ For Linux there is equivalent binary ]
Setup
Server Side:
On server side, you need to setup HTTP server. Without knowing specifics of your environment I can say that there are various solutions available , including python http server SimpleHTTPServer.
See solution here
Client Side :
Once http server is installed and running, on your laptop, you can simply run
C:\Python27\Lib\site-packages\gnome> .\gst-launch.exe playbin uri = http://www.freesound.org/data/previews/148/1482641337920-lq.mp3
Replace http server address with your server address and port.
You can call gst-launch in your python script and go creative with it.
OutPut:
I've opened audio files from freesound.org successfully. It should open a audio file being served from your http server as well.

Related

How to capture the traffic of an Android device in Python?

I want to capture traffic of a particular Android device using a Python script. I am doing the same thing for a laptop using "pyshark" that monitors the real-time traffic of laptop for a specific time and then creates a pcap file for it. I want to do the same thing for my Android device. I have tried Tcpdump, scapy and packetsniffer but nothing helped me in capturing the traffic of my Android device.
Ideally I want an analyzer (a python script) that should run in such a fashion that when it is turned on, it continuously monitors the traffic of my Android device. I will have to make something similar to a proxy server but i am stucked how to do it.
pyshark usage for my laptop
Does your program run with the necessary rights at all? Under Linux and Windows a program needs extended rights to open raw sockets, which are necessary for sniffing. Under Windows there must be some group policy XY setting (I don't know Windows), under Linux the program needs root rights.
I don't know about such a policy too. But in the scenario displayed in the picture attached, there is no need for socket programming. I am also okay with passive sniffing.

unable to flash micropython to esp 32: open serial error

issue: unable to flash micropython to esp 32: open serial error, please try again. hope to connect internet and try again.
pretty new to micropython - want to flash micropython to esp 32 ( resp 8266) board.
cf this docs: https://maker.pro/esp8266/tutorial/using-micropython-on-an-esp8266-with-upycraft
getting this errors all the time.
open serial error, please try again.
hope to connect internet and try again.
current version only open py txt json ini file.
hope to connect internet and try again.
hope to connect internet and try again.
i run Win 7
the hardware:
a. Joy-IT-Node MCU ESP8266
.. and besides that i also tried it with the following
b. ESP32-T Development Board CP2102 with Espressif ESP-WROOM-32 IoT WLAN & BLE Modul
yes: i have installed the > CP210x_Universal_Windows_Driver.zip
by the way: i could try to do all that on a linux machine. - on a MX-Linux-Sytem
any idea;
many thanks for any and all help in advance.
As of my opinion, You don't necessarily depend on the uPyCraft IDE. So there are several other ways to develop scripts and uploading them to Your board.
Some days ago I was facing problems with Python on the NodeMcu as well. Since I didn't like the tutorials around the web, I've set up one on GitHub, covering the end-to-end process (required software, drivers, firmware images, developing, flashing, uploading, etc.): python2nodemcu.
It depends on Mac and the NodeMcu board, but some of the topics may apply to other operating systems and boards as well.
And I think it covers the most relevant parts of the whole process, using the easiest tools around the web. Please have a look at it, but all in all it goes like this (short version of the linked documentation above):
Install Python 3
Verify with python --version
Or python3 --version
Install the correct SiLabs driver to enable serial USB-to-UART communication
See here
Find the device file (representing the interface to the physical device)
Try it using ls -la /dev/tty.*
Download the recent MicroPython firmware
You need the right one for Your board
Install the two Python-based libraries EspTool and PySerial
Try to erase the flash of Your device
Run python3 esptool.py --port /dev/tty.{device-file} erase_flash
Try to flash the original image or the MicroPython firmware to the board
Run python3 esptool.py --port /dev/tty.{device-file} write_flash 0x00000 {micropython-image-or-original-firmware}.bin
Try to connect to the board via the REPL prompt
Run screen /dev/tty.{device-file} 115200
You maybe need to try different baud rates (depending on Your driver, cable, etc.)
Connect to the board using Ampy
For example, try to list the files on Your device executing python3 ampy/cli.py --port /dev/tty.{device-file} --baud 115200 ls
Again, try other baud rates
Anyway, I've noticed that You do not necessarily need to push the physical flash button on the device.

Troubleshoot failed auto-reconnect in VLC network stream via settings or Python LibVLC API

I'm using VLC to stream a wireless IP camera to a wall-mounted monitor that's connected to a tiny Debian computer.
Please note that Auto-reconnect from Tools > Preferences > All > Input / Codecs > Access Modules > HTTP(S) is enabled already.
Problem: after 30 min - 1 hour the stream disconnects. Only the orange VLC cone displays.
Symptoms: I can easily reload it manually. The Auto-reconnect radio box had no effect.
Question: Is there a configuration, script-based, or other approach that I can leverage to automatically re-connect?
I am studying the CLI documentation and the Python bindings for LibVLC. I actually don't see anything useful in the documentation.
LibVLC and its Python bindings seem to provide a complete reproduction of VLC from the ground up, such as what you'd use to fork or embed the application. I'm sure there must be a way to use that somehow, but it's not apparent to me yet and this seems like an incredibly heavy-lifting approach to solving this bug.
Auto-reconnect in VLC did not work for me at all and I think this is still an issue. Therefore, I switched to Streamlink, which uses VLC. Here the process stops, if the stream disconnects. With a infinity loop you can let it automatically try to reconnect forever:
while :; do streamlink <link-of-stream> best -p vlc --retry-streams <seconds-until-retry>; sleep <seconds-until-retry>; done
I use it for YouTube streams. I am not sure, whether it also works for network streams.

Variable downloaded file size in a flask website

I generate an .odt file in a flask app using something like :
return send_file( # imported from flask
BytesIO(data), # importe from stdlib
attachment_filename=filename,
as_attachment=True
)
On my machine with Ubuntu using either Firefox or Chromium, I download the file fine. It opens like a breathe.
On another machine, on the same wired network but using a corporate windows setup and still Firefox, the file is missing bytes. The file size varies from download to download without any clear pattern, and is about 100/200ko smaller.
I tried with curl on said machine, and got the same results with missing bytes.
The server is a python 3.6 flask 0.12 powered website on a debian with gunicorn + nginx.
I found the problem: the machines where it doesn't work are the ones with kaspersky installed. If you disable it, the bytes are here. Now I have to find a way to tell the AV I'm not evil. I opened another question about it.

Control rs232 windows terminal program from python

I am testing a piece of hardware which hosts an ftp server. I connect to the server in order to configure the hardware in question.
My test environment is written in Python 3.
To start the ftp server, I need to launch a special proprietary terminal application on my pc. I must use this software as far as I know and I have no help files for it. I do however know how to use it to launch the ftp server and that's all I need it for.
When I start this app, I go to the menu and open a dialog where I select the com port/speed the hardware is connected to. I then enter the command to launch the ftp server in a console like window within the application. I am then prompted for the admin code for the hardware, which I enter. When I'm finished configuring the device, I issue a command to restart the hardware's software.
In order for me to fully automate my tests, I need to remove the manual starting of this ftp server for each test.
As far as I know, I have two options:
Windows GUI automation
Save the stream of data sent on the com port when using this application.
I've tried to find an GUI automater but pywinauto isn't supporting Python 3. Any other options here which I should look at?
Any suggestions on how I can monitor the com port in question and save the traffic on it?
Thanks,
Barry
Have you looked at pySerial? It's been a few years since I've used it but it was quite good at handling RS-232 communications and it looks like it's compatible with Python 3.x.
Sikuli might provide the kind of GUI automation you need.
I was also able to solve this using WScript, but pySerial was the preferred solution.

Categories