python3 -m http.server needing two terminals? - python

I'm going through the "Linux Fundamentals" section on TryHackMe, and I was asked to start a web server using Python's "HTTPServer" module and download a file from said server. My first line of code was python3 -m http.server to initiate the server, then in the Linux terminal all I had was a blank line, no directory or prompt or anything. I input wget http://ip.address:port/filename with the correct IP address, port, and filename but nothing ran. It took me opening another terminal window in order to download the file, is this correct or have I missed something? This is my first introduction to Linux so I apologise if I've missed something simple.

Related

pyInstaller and Django : Couldn't import Django

I am working on an django application and have kind of finish coding it.
This app is designed to be used by someone else and as she isn't so familiar with command line or so, i would like to create an executable (with pyInstaller) that start the localhost server and open the page on a browser (I don't nee to deploy it online or so, the localhost is sufficient).
To do so, I've created a python script that calls the command
python3 manage.py runserver
and then open the localhost page in a browser.
I then called the command
pyinstaller myApp.py
(with myApp.py the python script containing the 2 commands above) and then also the command
pyinstaller TestMACB2.spec
(I'm not sure I need this command but anyway I now have an executable that tries to run those two commands).
The opening of the web page works well but I dont know why for the command to start the server, i have this error:
Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
I do have django installed and when I start the server with the command in a terminal, it works fine.
This is myApp.py script :
import os
import sys
import webbrowser
os.system('python3 <full_path_to_manage.py> runserver')
webbrowser.open('http://127.0.0.1:8000/MACB')
Do you have an idea how to fix it ?
I also have another little issue : for the command os.system('python3 <full_path_to_manage.py> runserver') i must specify the full path. If i try something like ./manage.py or ./../../../manage.py it says it cant find this file. but in the second case it should work. do you know how I could avoid using the full path ?
Thank you for the time you took reading this!

CGIHTTPRequestHandler and SimpleHTTPRequestHandler in Eclipse on Mac in Firefox

Trying to run python cgi server from within Eclipse on a Mac Air and display hello world in Firefox, two problems. Here is the code to be run in a file run_server.py
from http.server import HTTPServer
from http.server import CGIHTTPRequestHandler
def run_server(handler_class,server_class=HTTPServer ):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
def main():
run_server(CGIHTTPRequestHandler)
if __name__ == '__main__':
main()
Directory structure under project directory
analytics
\
run_server.py
cgi-bin
\
index.py
index.py
#!/usr/bin/env python
print('Content-type: text/html;\n\n')
print('<h1>Hello, world!</h1>')
From a command line in the project directory, you can run with
$ python -m analytics.run_server
Be sure to have a __init__.py in the analytics directory to use -m option. Now try loading the page
http://localhost:8000/cgi-bin/index.py
In Chrome, things work. So what's the problem?
In Firefox, the url is never found.
On a Mac also, if you run the server from inside the Eclipse IDE, the cgi tries to run python 3 code with OS installed python 2
Firefox 404s, times out, presents a blank page, or if the url problem is solved, tries to save the file. It doesn't serve static content as well, when the url problem exists.
The Eclipse console in running the cgi, will display the stack trace of a syntax error from the python lib site.py print statement, tipping off the nature of the problem. See What's New in Python 3
Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)
Simple solutions to both follow, in the answer section below.
Add to or create file /etc/localhosts , as in sudo vi /etc/localhosts, or editor of your choice.
127.0.0.1 localhost
This fixes a known bug (link below) from two years ago, closed two months ago as a duplicate, and contains the above workaround, very near the bottom.
https://bugzilla.mozilla.org/show_bug.cgi?id=1433933
There is an active bug opened 5 years ago, updated 6 days ago, April 10, 2020, looks very near completion. https://bugzilla.mozilla.org/show_bug.cgi?id=1220810 Whenever it's done, you'll still have to update Firefox.
For the conflict of python versions, running the server from the command line presents no problem in executing the cgi, index.py However, trying to run the server inside Eclipse doesn't work, when the shebang path #! (etymology sharp bang, or shell bang) is
#!/usr/bin/env python
On a Mac, it picks up the default python 2 installation, but is trying to run python 3 code (assuming you've upgraded since python 2 is past end of life). Apple, despite OS updates has kept python 2 around for backwards compatability, though it's days are numbered. Eventually the OS and applications will no longer rely on any OS installed python.
Not sure of exact mechanism where Eclipse picks up python 2 when server runs script, though interpreter is 3. Setting path to #!/usr/bin/env python3 doesn't work, nor does #!/usr/bin/python3. What does? Find the absolute path to your python 3 and use that, it should appear in the error stack trace even, but here is an exampe from my setup.
#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

Create a command-line in Ubuntu

In Ubuntu, I'd like to create a command-line called pycharm_help which will open firefox or another browser with the website https://docs.python.org/2.7/py-modindex.html. I know for doing this, I need to create a script with #!/usr/bin at the beginning. As I'm starting doing programming, I'd like that someone could help me to create this script in python. Could anyone be able to tell me how to do it? And help me create this little program?
Thanks in advance!
There is standard module webbrowser to open page in default browser
#!/usr/bin/env python
import webbrowser
webbrowser.open("https://docs.python.org/2.7/py-modindex.html")
If you have to open in firefox then maybe you will have to use
#!/usr/bin/env python
import webbrowser
browser = webbrowser.get('firefox')
browser.open("https://docs.python.org/2.7/py-modindex.html")
BTW: Ubuntu will treat script as command-line command only if
it has in first line #! with program which it has to use to execute this script
(so called "shebang" or "hashbang" - # = she/hash, ! = bang)
ie. #!/usr/bin/env python or #!/usr/bin/python
(#!/usr/bin/perl, #!/usr/bin/php, etc.)
it has "eXecution" privilage:
chmod +x script.py
If you want to use a python script , you can follow the answer of #furas at comments.
But you can do it even in pure command line / bash script like this:
#!/bin/bash
xdg-open "https://docs.python.org/2.7/py-modindex.html" &
xdg-open calls the default web browser in your system.
Save the file (i.e charmhelp) under /usr/bin/ directory to be accesible from everywhere, then make it executable using chmod +x /usr/bin/charmhelp and you can run it when you need it as charmhelp
PS: If you save the file in other directory and you want to run it (i.e /name/home) you need to call it either by full path like /name/home/charmhelp or if you are already in name/home you have to run it as ./charmhelp (mind the dot in the beginning).
You could also use links (terminal web browser) directly from terminal like
links -dump "https://docs.python.org/2.7/py-modindex.html" |less
With links the web page will be displayed in terminal.
Much more simpler make an alias:
alias charmhelp='xdg-open https://docs.python.org/2.7/py-modindex.html &'
runit by charmhelp. To make the alias permanent you have to put it in name/home/.bashrc file .

Raspberry pi Python shebang with cgi server

I am trying to run a local CGI server on my raspberry pi to host a webpage with a single link, that link is to a CGI script which is supposed to trigger another script and then print HTML code to redirect back to the starting page (so that it doesn't hang)
in the servers root directory i have:
index.html
favicon.ico
Server.py
cgi-bin
my server is set up to use the cgi-bin folder for cgi-scripts.
the issue i am having is i cannot seem to make the scripts callable, is so instead of typing "python Server.py" i should be able to type "Server.py"
in order to do this i have tried multiple shebangs:
#!/usr/bin/env python
#!/usr/bin/python
and then called chmod a+x Server.py to mark it as executable, to no avail.
to clarify i am using:
python 2.7.3rc2
standard raspi linux distro "wheezy"
i read in some of the help docs that if the file has DOS style newlines it interferes with the shebang, so i have ensured that they are now MAC style newlines, this still did not work.
to test further i have made a simple python file which contains:
#!/usr/bin/python
print "Hello World!"
saved it as test.py, marked it as executable, and tried:
/test.py
from the command line and i get:
print: bad interpreter: No such file or directory
can someone please tell me where i'm going wrong?
Thanks
James
Try to remove windows line endings in the script. This made it work for me.
E.g. see How to convert Windows end of line in Unix end of line (CR/LF to LF)
For more possible causes of this problem have look at my answer here https://stackoverflow.com/a/65249192/1150303

Zsh/Bash: Path isn't what it should be

I stumbled upon something I just can't figure out. Following situation: I downloaded the python frontend to control dropbox via command line (dropbox.py). I put this file in the folder:
/home/username1/.dropbox-dist/dropbox.py
I made a simple bash script in /usr/bin called "dropbox":
#!/bin/bash
python /home/username1/.dropbox-dist/dropbox.py
Now when i run it following happens:
The whereis for the file:
root#linux_remote /home/username1 # whereis dropbox
dropbox: /usr/bin/dropbox
When i run it:
root#linux_remote /home/username1 # dropbox
zsh: no such file or directory: /home/username2/.dropbox-dist/dropboxd
Yeah. It tells me another username. To be specific: I'm logged in via SSH on this linuxbox. On the remote shell there is byobu running. In byobu runs zsh. Username2 equals the user that I'm currently logged in with on my local linuxbox, with which I connected:
username2#linux_local /home/username2 # ssh username1#linux_remote
Thats how I am connected.
So there must be a variable which was passed to my remote shell from my local shell, and python seems to read it, but I can't figure out which it would be.
Now.. look at that: When I type in the command that I wrote into the bash script:
username2#linux_remote /home/username2 # python /home/username1/.dropbox-dist/dropbox.py
Dropbox command-line interface
So it runs if I do it manually.
Another thing: If I run it with the whole path it works too:
root#linux_remote /home/username1 # /usr/bin/dropbox
Dropbox command-line interface
And it does work if I run it via login-shell, for example using "bash -l" and then trying to run "dropbox".
It doesn't work either if I change the hashbang to "#!/usr/bin/zsh"
Any ideas on this?
whereis doesn't do what you think: it searches a specific set of directories, not $PATH. which searches $PATH so you need to use which to find out which executable will be executed by a given name.
Edit: which as an external program (for shells that do not have a builtin command, such as bash) will not give a right answer for some cases, e.g. shell aliases. The type builtin should be used instead (it also should be available more widely as it's mandated by POSIX, though not necessarily as a builtin).

Categories