Http server showing the same page after refreshing? - python

I have a piece of code I am using for testing on http servers. What I did first was to set up my http server in python and type it in my web browser, listening on port8000. However when i delete theindex.htmland stopped thepython` script and hit refresh on my webbrowser, it still shows the same page! I am currently using Firefox developer edition by the way.
link to image
I have tried replacing the index.html with another different website index.html for testing, but it wasnt showing the new website.
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
I expect the web browser to stop showing index.html after closing the python script. Please help to fix my code so that it stops showing the same index.html

welcome to stackoverflow. I think your issue is with the caching! You can try hitting ctrl+f5 on your browser to see if it works.

Related

Access Flask application from local network

I want to be able to run Flask on a LAN connection without connection to the internet. When running the flask application it starts and runs on host = 0.0.0.0:5000 but the site can't be reached from browsers going to 0.0.0.0:5000. Running with an internet connection to the network allows the site to show up. When communicating from another device on the same network sending a post to the Flask's ip and correct endpoint allows for a successful http post request with response when the network does not have internet.
The main function is shown here.
if __name__ == "__main__": port = 5000 app.run(port = port,host = HOST, debug = True)
Bottomline: How do I run flask with a wifi connection that does not have internet to communicate with other devices on the network and show the site at 0.0.0.0:5000? thanks
its 127.0.0.1:5000 from your same pc or the local ip like 192.168.1.x from other pcs on the same lan, check your ip with ipconfig
Change the code to this:
if __name__ == "__main__":
app.run(host='127.0.0.1', debug=True)
127.0.0.1 is the localhost of your computer and does not require internet connections, besides, the address is used to establish an IP connection to the same machine or computer being used by the end-user. So it still can run even without the internet.

Cannot connect to my tcp tor hidden service in Python

I have a fully functional tor hidden service with his v3 url, created with stem and the controller class. The server is redirecting the traffic into a local tcp server running on port 5000 created with socket. But now I want to connect to this hidden service using python. So I tried socks with SOCKS5 proxies, no way of making it work. I also tried the torpy library with the example provided on the offical doc but, again, no way to make it works:
from torpy import TorClient
url = 'myv3torurl.onion'
tor = TorClient()
with tor.create_circuit() as circuit:
print('circuit done')
with circuit.create_stream(('http://' + url, 5000)) as client:
print('sending')
client.send(b'yeah yeah im there')
print(client.recv(1024))
Also tried changing the url adding http:// or https://, or tried to change the port with 80, 9050 or 9051. The torpy logs are always the same and you can find them here.
At this point I do not really know what to try. I just want a tcp connection hosted by the onion website. And by the way, I'm sure it's not a problem of the server because if I try to copy and paste the url on the tor browser it is fully reachable. Thank you for any help.
Noticed that torpy page says it is for v2 services and yours is v3.

I can't connect to my Bottle server on external devices

I'm trying to get a Bottle server in Python to work. Here is my code:
from bottle import route, run, template
from socket import gethostname, gethostbyname
from time import sleep
ip = str(gethostbyname(gethostname()))
#route('/')
def index():
return 'Hello World!'
run(host=ip, port=1234)
I run this, and on my computer where I'm running it, I navigate to http://127.0.1.1:1234/, and my website shows up, with Hello World!.
However, if I try to connect to it on my phone or my sister's Chromebook, it says that the website refused to connect.
I have tried replacing str(gethostbyname(gethostname())) with '0.0.0.0' and 'localhost', but none have worked.
Get rid of this line; it's not necessary:
ip = str(gethostbyname(gethostname()))
Make your run line look like this:
run(host='0.0.0.0', port=1234)
The address 0.0.0.0 means "listen on all addresses".
Lastly, figure out the network address of the host on which your app is running. Then other devices on the same network should be able to connect to <that ip address>:1234. Devices not on the same network would only be able to connect to the service if you had a publicly routeable address (or if you arranged to forward the appropriate port from a router that has a public address).
You'll want to make sure the system on which your app is running doesn't have firewall rules that would prevent an otherwise successful connection.

Connecting to Python socket from iOS Safari browser

I have a python socket listening on my computer's ip address on a specific port. I am using the standard python socket library with something like the following code:
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(server_address) # Server address is a tuple (HOST, PORT) with host being
# something like 123.456.789.00 and port being 4000
listen_socket.listen()
client_connection, client_address = listen_socket.accept()
request_data = client_connection.recv(1024).decode('utf-8')
print(request_data)
When I use Chrome on my iPhone to connect to the address, the rest of my code runs fine and processes the incoming request by sending an html page. When I use Safari, it loads for a while until it fails and a says "Safari cannot open the page because it could not connect to the server."
Funny enough, if I stay on the page, restart the server, and then reload the page using the refresh button, it connects fine. But trying to access the address through the url address does not work. Refreshing using the refresh button seems to work. Why is Safari having trouble connecting to my Python socket?
Try increasing the backlog count in the listen_socket.listen() call.
I had a similar issue with a python 2 socket server and macOS Safari. Safari would say it could not connect to the server, but Chrome/Firefox/IE/curl all worked without any errors. I changed this:
listen_socket.listen(1)
To this:
listen_socket.listen(5)
Safari then worked without any errors. I am now using 128 instead of 5, not sure what the default is in Python 3.
When set to 1, in the python server I would see Safari connect, but then socket.recv() would never return.

When to use route_url or route_path in Pyramid?

I have an issue with route_url and my setup. On the server I have a paster server which listen on 127.0.0.1 on port 6543 and a nginx server which does reverse proxying from port 80 to port 6543.
I'm also using paste prefix to retrieve the real client IP with this setup in my ini file:
[filter:paste_prefix]
use = egg:PasteDeploy#prefix
[pipeline:main]
pipeline =
paste_prefix
myapp
The server is on a private LAN and I'm trying to connect to the server through a SSH tunnel set up as this:
ssh me#sshgateway -L 8080:nginx_server_ip:80
And I connect to the web page on my client at this url: http://localhost:8080
The main page is displayed correctly but then all links generated with request.route_url are redirecting to localhost/url (without the :8080).
I guess this have something to do either with nginx or paste prefix (or both).
I hope that replacing route_url with route_path will probably solve this problem without fixing the nginx/ini setup issue.
Is there any reason to call route_url instead of route_path, ever ?
route_url is useful in situations like generating a redirect from HTTP to HTTPS, or to a different subdomain. Other than that route_path is probably preferable.

Categories