flup and socket connection

1k views Asked by At

I'm trying to connect to my test fcgi application via unix socket. The server is pretty simple and taken from flask documentation http://flask.pocoo.org/docs/0.10/deploying/fastcgi/

The client code is mine. I expect to send the sample HTTP request via socket.send and and receive an HTTP response with "Hello World" into it. The problem is that client hangs up on s.recv(1024) and nothing happens.

The server code:

def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World!\n']

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    WSGIServer(myapp, bindAddress="fcgi.sock").run()

The client code:

# -*- coding: utf-8 -*-
import socket

req = """GET / HTTP/1.1
Host: localhost\r\n\r\n"""

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(1.0)

s.connect("fcgi.sock")
s.send(req)

data = s.recv(1024)
s.close()

print('Received ' + repr(data))

I tested to get response through the nginx, and it worked well, but I cannot to perform this using sockets. Does anyone know what is the problem with my client code?

1

There are 1 answers

0
Ilya Boltnev On

If anybody interested, I figured out what was the problem.

It was naive to think that FCGI server would receive raw HTTP request string and respond to it correctrly. FCGI server is not designed to process HTTP, it is purposed for FCGI requests, which have other format: http://www.mit.edu/~yandros/doc/specs/fcgi-spec.html

That's why it was hanging on recv - the server simply cannot response to such type of requests.

The python flup package have fcgi client into it. If you ever wanted to test your fcgi application, you can use it.