Socket programming python on real server - python

I'm following this http://www.raywenderlich.com/3932 for socket programming in iOS where the server coding is in PYTHON, however, I just want to know that according to this tutorial, the author used localhost and run the code from terminal such that python server.py to execute and listen for socket.
What I'm confusing is that, how can I make this command on real server, such that after putting the code of python in CGI-BIN, how can I run that from shell/terminal of a shared web hosting.
Here's my SSH Screenshot, where I tried to run that command to bind and listen for socket, but Here i'm failed as no JAVA LOGIN section is appearing in my case as the video tutorial shows.
My Question is, How can I run the command so that the server will listen for the port, as on my localhost.
The command is: python server.py

On a shared web hosting server you probably have a running web server for which you write scripts which generate some output for the web server to return to the client.
server.py however is no such script. It contains the code for an actual server. Running the command starts the server. Therefore you won't get this working by simply putting the file in a CGI-BIN folder. You do need to run the command.

Related

How do i automatically run python code when my localhost apache server starts up in windows

I am trying to run a local host server using laragon .I want to start a python program and keep it running(even after server restart) until my server shutdowns .
Suppose i have a file test.py , I want to keep it running as soon as my server starts up .
I know it might work when used with PHP and calling that script using the browser. But i want to run it as soon as the server starts up as a python program.

Python server with library socket

Im trying to build a python application that will be run on a specific port so that when i try to connect on that port the python application will be run.
Im guessing i have to do that with socket library but im not very sure about that.
On Linux you can do this with xinetd. You edit /etc/services to give a name to your port, then add a line to /etc/xinetd.conf to run your server when someone connects to that service. The TCP connection will be provide to the Python script as its standard input and output.

Python connection refused from localhost when runningn on cron

This is funny because most questions I found are the other way around.
I have a flask server that I start on debugging mode, listening on port 6000.
I have a script that accesses the url through localhost which sends back a JSON reply.
When I do an ssh tunnel using ssh -L and run the script that sends a request in my machine to localhost everything works fine. When I run the script directly from the server it also works fine. However, since I want to ultimately use a big data set, I setup a cron job. When the cron runs, the script crashes with connection refused error. I can see that the server is running after the script crashes and the port is correct on both.
I have no idea why this can happen. Any ideas are appreciated.
Solution
I couldn't figure out the reason or how to solve it, so I decided to run manually and put it in the background.
How to make a programme continue to run after log out from ssh?

Error accessing my webpage from network

I've just started learning network developing using Flask. According to its official tutorial:
Externally Visible Server
If you run the server you will notice that the server is only
accessible from your own computer, not from any other in the network.
This is the default because in debugging mode a user of the
application can execute arbitrary Python code on your computer.
If you have the debugger disabled or trust the users on your network,
you can make the server publicly available simply by adding
--host=0.0.0.0 to the command line:
flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.
However, when I try to access 0.0.0.0:5000 on another device, I got an error: ERR_CONNECTION_REFUSE. In fact, I think this behavior is reasonable, since people all around world can use 0.0.0.0:5000 for different testing purposes, but isn't the tutorial implying that adding --host=0.0.0.0 can make my webpage "accessible not only from your own computer, but also from any other in the network"?
So, my question is:
What does adding --host=0.0.0.0 do?
How can I access my webpage on device B while the server is running on device A?
You don't access the Flask server on another computer by going to 0.0.0.0:5000. Instead, you need to put in the IP address of the computer that it is running on.
For example, if you are developing on a computer that has IP address 10.10.0.1, you can run the server like so:
flask run --host=0.0.0.0 --port=5000
This will start the server (on 10.10.0.1:5000) and listen for any connections from anywhere. Now your other device (say, on 10.10.0.2) can access that server by going to http://10.10.0.1:5000 in the browser.
If you don't have the host=0.0.0.0, the server on 10.10.0.1 will only listen for connections from itself (localhost). By adding that parameter, you are telling it to listen from connections external to itself.

Connection interrupted when run "indirectly". Bottle.py on Ubuntu

I'm running a local web service on Ubuntu on localhost:8090, written with bottle.py.
The connection uses SSL.
If I execute the main.py file from Nautilus or the terminal and connect to https://localhost:8090 everything works fine.
When I execute it from a link to the file, an .sh script or a .desktop file the server starts running fine, but when I browse to the address firefox says "The connection to localhost:8090 was interrupted while the page was loading"
$telnet 127.0.0.1 8090 gives this:
Trying 127.0.0.1...
Connected to 127.0.0.1...
Escape character is '^]'.
Connection closed by foreign host.
$sudo netstat -ntlupp | grep 8090 gives this:
tcp 0 0 127.0.0.1:8090 0.0.0.0:* LISTEN
iptables is default
I've got the feeling it's blocking the connection when the server is executed "indirectly" (link, script or .desktop), since when I actually click on the file or run it through terminal it runs fine.
I don't have a clue on where to prevent it from blocking the connection, though. Any help is greatly appreciated.
Any workaround will do, even just pretending the file is being run directly from the user.
Thanks in advance
Watch the server logs.
The major difference between the different methods of invocation probably is the current working directory.
I think that it is unlikely that the network configuration is involved in what you are observing.
Depending on the complexity of your web application it might be that a Python import fails if the main script is not run from the right directory. This would trigger a Python exception, which might lead to an immediate connection reset. I have not worked with bottle, but other Python web frameworks distinguish a development mode in which Python tracebacks are shown in the browser, and a production mode in which an HTTP error is sent to the client.
This is what you should do in order to debug your issue: run your server from a terminal (cd to the right directory, then run python application.py). Carefully watch stdout and stderr of that server process while connecting to the web application with your browser.
Ok, problem solved.
It was actually depending on the current working directory not being the same as the python file running the WSGI server.
If I run the .sh script or the link from the same directory everything works fine, and if I give a cd command in the script everything works smoothly.
Thanks for the help Jan-Philip!

Categories