This question already has answers here:
Can I run two web servers on the same computer?
(7 answers)
Closed 2 years ago.
can i run multiple python http servers on one machine to receive http post request from a webpage?
currently i am running an http server on port 80 and on the web page there is a HTML form which sends the http post request to the python server and in the HTML form i am using the my server's address like this : "http://123.123.123.123" and i am receiving the requests
but i want to run multiple servers on the same machine with different ports for each server.
if i run 2 more servers on port 21200 and 21300 how do i send the post request from the HTML form on a specified port , so that the post request is received and processed by correct server??
do i need to define the server address like this : "http://123.123.123.123:21200" and "http://123.123.123.123:21300" ?
Yes can run multiple webservers on one machine.
use following commands to run on different ports:
python3 -m http.server 4000
4000 is the port number, you can replace it with any port number here.
Related
This question already has answers here:
Get IP address of visitors using Flask for Python
(12 answers)
Closed 3 months ago.
I am building an application that requires the client ip for geolocation purposes. I am using python, flask, and nginx to serve. From what I have read, common ip address capturing happens in the actual server. Any python script I use inevitably just returns my servers ip. What is the best way to get a client's ip address and pass it to the python scripts for further work to be done?
Although you're required to share part of your code to get help, I'm gonna answer your question: You can use Flask request.
from flask import request
...
client_ip = request.environ['REMOTE_ADDR']
client_port = request.environ['REMOTE_PORT']
This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 1 year ago.
I've built a restAPI using Flask and tested it in local device. I want to host server in my local device(my desktop) and want to send request from another device(my laptop).
Searching for this gave me lots of resources on how to host server on a platform or on internet. I just want to test before hosting it elsewhere.
EDIT:
from my laptop:
path = f'http://{hosted_server_ip}:{hosted_server_port}/{endpoint}'
requests.post(path, json=input_data)
outputting ConnectionRefusedError
For this you first need both the laptops connected to same WiFi (or any other local network)
Then use your desktop IP address assigned by the router (in place of localhost in url)
To find out the IP address of your system:-
Type into the command line ipconfig (Windows)
There's going to be a bunch of IP's
Try all of them except 127.0.0.1
This question already has answers here:
How to send http requests to flask server
(2 answers)
Closed 4 years ago.
I need to send a request to a web server of mine to start a stream. The web server is located at 0.0.0.0 (of course I can change the address).
How can I send a "GET" request to that server?
I already tried using httplib or urllib2 or 3 and they seem not to work with IP address.
I know that a local DNS server will map the address to a url, but that is not the goal to set up the server every time I want to execute the code in a new network.
Thank a lot.
You could use requests:
requests.get('http://0.0.0.0')
or even better
s = requests.Session()
s.get('http://0.0.0.0')
r = s.get('https://httpbin.org/cookies')
to keep the connection persistent, which is probably more like what you want.
See more about requests sessions at http://docs.python-requests.org/en/master/user/advanced/
Or you could just convert the IP address to a hostname using
socket.gethostbyaddr(ip) with urllib
to convert the IP address to a host name
It doesn't work because you probably haven't included the web protocol to use (i.e. HTTP or HTTPS). Try it like this
import urllib2
urllib2.urlopen('http://0.0.0.0')
I have been using parse as my project server and now that I have migrated the data to local server, successfully made dashboard work; but I cannot find a way to access api.parse.com/1/ API.
I used to use python to make REST requests and it is basically establishes socket connection with api.parse.com at port 443. Now I am trying to connect to localhost at port 1337 which is where the parse-server instance is running. However, I have not been able to access the API same as before.
One thing to note is that I can successfully curl to get basic JSON response from requests like
curl -X GET -H ... http://localhost:1337/parse/classes/_User
The question is which connection now replaces api.parse.com for locally held parse-server instances?
Ahh I found out the answer:
You make socket connection with address and port where the parse-server is running, and then instead of doing
conn.request("GET", "/1/login?%s"%...)
You do:
conn.request("GET", "/parse/login?%s"%...)
Hope this helps.
I am new to Twisted and started using treq because of its similarity to Requests (very easy to use basic authentication etc). I have an HTTPConnectionPool with maxPersistentPerHost=1 and persistence=True, I send 4 requests in a row to a host: treq.put(1), treq.get(1), treq.put(2) and treq.get(1). However the apache server running on the host receives the requests out of order (checked /var/log/apache2/access.log). Using netstat I saw 4 connections to the host, I was expecting only 1 connection and all requests to go over the same connection in order. Any idea on what I am doing wrong or missing?
Thanks!
Reshad.