Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 728
Michael Ekoka Avatar asked Sep 15 '25 14:09

Michael Ekoka


1 Answers

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

like image 163
roberto Avatar answered Sep 18 '25 07:09

roberto