vendredi 7 avril 2017

Python execute script from cgi directory

I want to write a web server which gets parameters(name) from the URL and send back a page containing "Hello + name".

Here is my server.py

#!/usr/bin/env python
# coding: utf-8

import socket
import cgi

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('', 8021))

message = """
HTTP/1.1 200 OK
"""

while True:
        socket.listen(5)
        client, address = socket.accept()
        print "{} connected".format( address )
        response = client.recv(1024)
        if "cgi-bin" in response :
            print response[response.find("n/")+2 : response.find("?")]

        print response
        client.sendall(message)


print "Close"
client.close()
socket.close()

URL for testing:

localhost:8021/cgi-bin/hello?name=jihane

I created a directory cgi-bin at the root with the program hello.py that prints "Hello+name"

I would like to know how to run hello.py in my server.py.

Thanks!




Aucun commentaire:

Enregistrer un commentaire