mercredi 4 janvier 2017

Get argument in python web server

Thanks for attention, friends. I have created simple web server in python, I found the code, then modified it to use HTML, I don't want to use frameworks(CGI, Flask, Django, Pyramid and etc.), but I want to get arguments from web browser, for example get text from HTML forms(textbox, submit button...) Here is the code:

import socket

HOST, PORT = '', 8888

listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print 'Serving HTTP on port %s ...' % PORT
while True:
    client_connection, client_address = listen_socket.accept()
    request = client_connection.recv(1024)
    print request
    if request.startswith("GET /hi"):
        http_response="""\
HTTP/1.1 200 OK\nContent-Type: text/html\n\n
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Zviadi</p>
</body>
</html>
"""
    else:
        http_response="""\
HTTP/1.1 200 OK\nContent-Type: text/html\n\n
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Zviadi2</p>
</body>
</html>
"""
    client_connection.sendall(http_response)
    client_connection.close()

Thank you

Aucun commentaire:

Enregistrer un commentaire