Can I use the uwsgi protocol to call http?

2019-03-01 10:40发布

问题:

Here's a data flow: http <--> nginx <--> uWSGI <--> python webapp

I guess there's http2uwsgi transfer in nginx, and uwsgi2http in uWSGI.

What if I want to directly call uWSGI to test an API in a webapp?

actually i'm using pyramid. just config [uwsgi] in .ini and run uWSGI. but i want to test if uWSGI hold webapp function normally, the uWSGI socket is not directly reachable by http.

回答1:

Try using uwsgi_curl

$ pip install uwsgi-tools

$ uwsgi_curl 10.0.0.1:3030 /path

or if you need to do some more requests try uwsgi_proxy from the same package

$ uwsgi_proxy 10.0.0.1:3030
Proxying remote uWSGI server 10.0.0.1:3030 "" to local HTTP server 127.0.0.1:3030...

so you can browse it locally at http://127.0.0.1:3030/.

If your application allows only certain Host header, you can specify host name as well

$ uwsgi_curl 10.0.0.1:3030 host.name/path

$ uwsgi_proxy 10.0.0.1:3030 -n host.name

If application has static files, you can redirect such requests to your front server using -s argument. You can also specify different local port if needed.



回答2:

First, consider those questions:

  • On which port is uWSGI running?
  • Is uWSGI running on your or on a remote machine?
  • If it's running on a remote machine, is the port accessible from your computer? (iptables rules might forbid external access)

If you made sure you have access, you can just call http://hostname:port/path/to/uWSGI for direct API access.



回答3:

From your question I'm assuming, you want to directly run your WSGI-compliant app with uWSGI and open an HTTP-Socket. You can do so by configuring your uwsgi.ini (or whatever the filename is) with

http=127.0.0.1:8080

uwsgi will now open an HTTP-socket that listen on port 8080 for incoming connections from localhost (see documentation: http://uwsgi-docs.readthedocs.org/en/latest/HTTP.html)

Alternatively you can directly start your process from the command-line with the http-parameter:

$ uwsgi --http=127.0.0.1:8080 --module=yourapp:wsgi_entry_point

If you use unix-sockets to configure uwsgi nginx is able to communicate with that socket via the uwsgi-protocol (http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html).

Keep in mind, that if you usually serve static content (css, javascript, images) through nginx you will need to set that up, too, if you run uwsgi directly. But if you only want to test a REST-API this should work out for you.