#CGIHTTPServer
Explore tagged Tumblr posts
sydneycbdrepaircentre · 7 years ago
Text
❤ How to Start a Simple Web Server in Python 3 on Mac
New Post has been published on https://is.gd/qfVR4P
❤ How to Start a Simple Web Server in Python 3 on Mac
      If you’re a Python user you may already be familiar with the handy trick that allows you to instantly create a simple web server using an easy command string entered in the command line of Mac OS. But if you’re a Mac Python user who has installed or updated to Python 3, then you will find the traditional command string from the prior Python versions does not work to initiate the web server in new Python 3.x+ releases.
Not to worry, the simple web server Python trick still works in Python 3 for Mac (and for Linux and Windows too of course, but we’re obviously covering MacOS), it’s that the command syntax is just slightly different. We’ll show you how to start a simple web server with Python 3 by using the new Python 3.0+ equivalent of the python -m SimpleHTTPServer command.
How to Start a Web HTTP Server in Python 3.0+
  We’re assuming you have already installed or updated to Python 3.0+ on the Mac, this variation of the command requires Python 3.0 or newer.
From the command line, enter the following syntax exactly*:
python -m http.server
OR (depending on how Python 3.x is installed and named):
python3 -m http.server
Hit return and Python 3 will instantly start a simple HTTP server from the directory in which the command was executed.
The http.server in Python 3 will run in the terminal, if there is no web file in the directory than the directory index itself will be shown.
You can test this immediately by opening the following URL in any web browser on the computer:
http://0.0.0.0:8000
All web server activity, like accessing individual files, folders, directories, etc, will be shown in the active Python terminal window live as it happens, sort of like tailing web logs on an Apache or Nginx server.
* Important Note: if you have python and python3 installed concurrently, you may need to change the syntax slightly to reference python3 and python2 or another python version. This will depend on how you updated to Python 3 on the Mac, but a common example would be using the ‘python3’ command instead:
python3 -m http.server
Just as before, hit return and the active directory will be turned into a web server.
        What’s the Python3 equivalent of python -m CGIHTTPServer?
  Another common trick is to use the CGI (Common Gateway Interface) server in Python for CGI scripts in python or perl. Thus if you need to run the python3 equivalent of the “python -m CGIHTTPServer” command for CGI that would be as follows:
python3 -m http.server --cgi
Alternatively, if Python 3 is installed and named as python, the command would simply be:
python -m http.server --cgi
Either way you’ll need the –cgi flag to start the CGI HTTP server in Python 3.
  I get an error saying “/usr/bin/python: No module named http” now what?
  If you see an “/usr/bin/python: No module named http” error when trying to execute the python -m http.server command string then you most likely are not running Python 3, or you are using the wrong command for python3 (i.e. python vs python3, depending on how the version is named and how it was installed or updated to on the Mac). It’s also possible that Python is not installed on the computer at all, though that’s less likely for Macs because Python2 is installed by default on Mac OS, though users must manually install the updated Python 3.x on the Mac, which simultaneously preserves the original Python 2.x release version. Typically this is achieved with Homebrew.
Change the syntax as referenced above, or if you’re using an earlier Python version try the “python -m SimpleHTTPServer” command from Python 2 and before.
Of course the python simple web servers are not meant for production environments and they’re really just best for a quick scratchpad or test environment. If you want to run a web server that is publicly facing or just more robust in general, you’d want to go with something like Apache or Nginx, though configuring those individually on a Mac requires some setup. A simpler option for an full web server environment on the Mac is to use MAMP, which makes setting up and starting a full Apache, MySQL, PHP environment on the Mac about as simple as it gets.
0 notes
ttrftech · 13 years ago
Text
Webブラウザで144MHzのアマチュア無線を受信してみる
RTL2832Uドングルで使うrtl-sdrには、rtl_tcpのほかにもいくつかコマンドが含まれています。そのなかでもrtl_fmというコマンドはその名から予想されるとおり、FMを受信することができます。これはFM放送だけではなく、アマチュア無線のような狭帯域NBFMも復調できます。実はヘルプを見てみるとAMやSSB(LSB/USB)、果てはCWの復調やI/Q信号を取り出すことまで可能なようです。復調周波数や復調モードなどはすべてコマンドライン引数で指定することができます。
別報のとおりrtl_tcpによるリモート受信サーバの実験をしていたのですが、その流れでrtl_fmの仕様を眺めていたところ、これをHTTP CGIにすることを思いつきました。rtl_fmが標準出力に書き出すRAW AUDIOをブラウザがサポートするフォーマットに変換することができたならば、HTTPを経由して受信した音声をブラウザで再生させることができそうです。HTTPによる受信サーバになるわけです。
Tumblr media
Linuxでは音声フォーマットの変換にsoxを使うのが定番です。soxは標準入力からRAW AUDIOを受け取って、WAVに変換することができます。あとはHTTPヘッダを付けることでCGIとして必要最小限の機能を満たすことができます。b-shellだとこんな感じになります。QUERY_STRINGで周波数を渡すようにしてみました。デフォルトでは145MHzになるようにしています。このCGIにアクセスするためにHTMLも用意します。以下を参照してください。
このCGIを使うためには、RTL2832Uドングルを接続したLinuxで、HTTPサーバを動作させる必要があります。一般的なApacheなどのWebサーバを使っても良いのですが、ここでは簡単にpythonに含まれているサーバを使ってみます。debian(raspbian)には最初からインストールされています。
ディレクトリを作成して、その中にHTMLファイルとCGI用のディレクトリcgi-binを作成し、先ほど作成したスクリプトを配置します。最後にpythonを使って、CGIをサポートしたHTTPサーバを起動します。HTTPサーバはフォアグラウンドで動作し、ポート番号はデフォルトで8000となります。
$ mkdir web $ cd web $ mkdir cgi-bin $ cp ~/nbfm2wav.cgi cgi-bin $ chmod +x cgi-bin/nbfm2wav.cgi $ cp ~/nbfm.html . $ python -m CGIHTTPServer
HTTPサーバが動いたら、PCやMacのブラウザからアクセスしてみます。
Tumblr media
Startボタンを押すと、5秒ほど経過した後に音声が再生されるはずです。再生開始が遅れるのはブラウザでバッファリングされるためです。MacのChromeを使った場合で5秒程度でした。
周波数を変更したときも、やはり5秒程度遅延します。バッファリングがある以上これは仕方ありません。
http://youtu.be/3v12uJ7xfnw
最近は、Webアプリのようにユーザインターフェースをブラウザで構成することが標準的な手法となっていますが、ソフトウェア無線でも可能かもしれません。今回は原理の確認ですので、簡単なことしか試していませんが、復調モードやゲイン、スケルチほか、受信機に必要な各種操作ができるようなユーザインターフェースを作ることは難しいことではなさそうです。手元には特別なクライアントを必要とせず、オーディオ(HTML5)に対応したブラウザがあればOKです。また、PCだけではなく、スマートフォンからの利用も可能だと思われます。(残念ながら上記のスクリプトではうまくいきません)
前回と同様Linuxとして、Raspberry Piを利用しています。rtl_fmでFM復調した場合CPUを30%消費していました。
rtl_tcpの受信サーバはサンプリングレート2Mで8bitのI/Q信号を送信しますので、16〜32Mbps程度の帯域幅を消費します。この記事のHTTP受信サーバは音声のみをサンプリングレート16kHz16bitで送信しますので256kbps程度の帯域幅になります。
この方法によるリモートサーバは、同時に一人しか使用することができません。HTTPセッション毎にrtl_fmがデバイスを占有してしまうためです。工夫すれば複数のセッションをサポートするようにはできますが、周波数やモードを個別に設定することはできません。複数のドングルを搭載したとしても、サポートできるユーザ数はドングルの数に限定されます。ブラウザで利用できるとは言っても、不特定多数にサービスすることは難しいです。LAN内で利用することが現実的です。
上で述べたように音声の再生にバッファリングによる遅延が生じます。このため、チューニング操作をしようとした場合は、操作から音声が変化するまで遅延が生ずることとなり、連続的なチューニング操作は容易ではありません。現状ではSSBやCWなどの利用はスムースにはいきません。ラジオのようなチャネル設定による利用が最善かもしれません。
エアバンド(AM)を同様に受信した例。ブラウザのアドレス欄で直接周波数指定しています。http://youtu.be/ogm6XB7v09A
rtl_tcpによるリモートサーバについての前回の実験 http://ttrftech.tumblr.com/post/36742746471/raspberry-pi-remote-rtl2832u-sdr-server
18 notes · View notes