简体   繁体   中英

Writing to a uWSGI unix socket

I have a Python wsgi app that is served by uWSGI behind NGinx. NGinx listens on the network and forwards requests to the uWSGI unix socket located in /tmp/uwsgi.socket .

Now, I'm trying to emulate what I'm speculating NGinx does when talking to this socket. I've tried the following using Python:

import socket

uwsgi_socket_address = "/tmp/uwsgi.sock"
socket_client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
socket_client.connect(uwsgi_socket_address)
msg = "GET /index HTTP/1.1\r\n"
socket_client.sendall(msg)

I get this error in return

/usr/lib/python2.7/socket.pyc in meth(name, self, *args)
    222 
    223 def meth(name,self,*args):
--> 224     return getattr(self._sock,name)(*args)
    225 
    226 for _m in _socketmethods:
error: [Errno 32] Broken pipe

1) is what I'm trying even possible with that socket and uWSGI? if so what is missing to get this to work?

2) is there a python utility that can help me in crafting text http requests, rather than just querying the server themselves on my behalf? Like so :

>>> import somehttplib
>>> http = somehttplib()
>>> request = http.get('/index')
>>> request.text=='GET /index HTTP/1.1\r\n'
True 

You cannot talk HTTP to a server speaking the 'uwsgi' protocol. The uwsgi protocol is a simple binary serialization format for arrays and dictionaries. If you want to speak HTTP with the uWSGI server you have to configure it to speak HTTP with --http-socket instead of --socket.

You can have both in the same instance with:

uwsgi --http-socket <address1> --socket <address2> ...

Eventually you can easily implement a uwsgi serializer with few lines.

This is an example of a parser (you need the opposite obviously):

https://github.com/unbit/blastbeat#uwsgi

Here you find the specs:

http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html

Have a look at uwsgi-tools package. It uses IP sockets, but with some small changes you can apply it to unix sockets as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM