Python ve Cgi Scripting Giri Python CGI ile

  • Slides: 15
Download presentation
Python ve Cgi Scripting Giriş

Python ve Cgi Scripting Giriş

Python CGI ile Programlama • Common Gateway Interface, veya CGI, HTTP sunucuları gibi bilgi

Python CGI ile Programlama • Common Gateway Interface, veya CGI, HTTP sunucuları gibi bilgi sunucuları ile arabirim harici ağ geçidi programları için bir standarttır. • CGI (Common Gateway Interface), Web Servisleri ile bu servislerin dışındaki programlar arasında etkileşim (ortak çalışma) platformu oluşturmak için geliştirilmiş bir standarttır. CGI, Web'in statik yapısına, HTML kodu içinden çağrılan CGI programları dinamik bir nitelik kazandıran programdır.

 • CGI programları “çalıştırılabilir” kodlar olduğundan, bir başkası, sizin sisteminizde program çalıştırabilir

• CGI programları “çalıştırılabilir” kodlar olduğundan, bir başkası, sizin sisteminizde program çalıştırabilir

 • Python klasörünün altında cgi-bin oluşturun. İçine hello. py koyun. print 'Content-Type: text/html'

• Python klasörünün altında cgi-bin oluşturun. İçine hello. py koyun. print 'Content-Type: text/html' print '<html>' print '<head><title>Hello from Python</title></head>' print '<body>' print '<h 2>Hello from Python</h 2>' print '</body></html>‘

 • • • Cmd prompttan Python –m http. server –cgi Yazın. Web browser

• • • Cmd prompttan Python –m http. server –cgi Yazın. Web browser açın: http: //localhost: 8000/cgi-bin/hello. py yazın

import textwrap from six. moves. Base. HTTPServer import Base. HTTPRequest. Handler, HTTPServer class Hello.

import textwrap from six. moves. Base. HTTPServer import Base. HTTPRequest. Handler, HTTPServer class Hello. Request. Handler(Base. HTTPRequest. Handler): def do_GET(self): if self. path != '/': self. send_error(404, "Object not found") return self. send_response(200) self. send_header('Content-type', 'text/html; charset=utf-8') self. end_headers() response_text = textwrap. dedent(''' <html> <head> <title>Greetings to the world</title> </head> <body> <h 1>Greetings to the world</h 1> <p>Hello, world!</p> </body> </html> ''') self. wfile. write(response_text. encode('utf-8')) server_address = ('', 8000) httpd = HTTPServer(server_address, Hello. Request. Handler) httpd. serve_forever() Request Handler Kullanmak

Flask ile CGI (pip install flask) from flask import Flask app = Flask(__name__) @app.

Flask ile CGI (pip install flask) from flask import Flask app = Flask(__name__) @app. route("/") def hello(): return "Hello World!" if __name__ == "__main__": app. run()

App. py from flask import render_template @app. route('/hello/') @app. route('/hello/<name>') def hello(name=None): return render_template('index.

App. py from flask import render_template @app. route('/hello/') @app. route('/hello/<name>') def hello(name=None): return render_template('index. html', name=name)

İndex. html <!doctype html> <title>Hello from Flask</title> {% if name %} <h 1>Hello {{

İndex. html <!doctype html> <title>Hello from Flask</title> {% if name %} <h 1>Hello {{ name }}!</h 1> {% else %} <h 1>Hello, World!</h 1> {% endif %}

Örnek : Flask Paketi kullanan Klasör. Yapısı • /hello_flask. py /templates/ basepage. html middle.

Örnek : Flask Paketi kullanan Klasör. Yapısı • /hello_flask. py /templates/ basepage. html middle. html result. html /static/ main. css

basepage. html <!doctype html> <head> <title>{{ page_title }}</title> <link rel="stylesheet" href="static/main. css"/> </head> <body>

basepage. html <!doctype html> <head> <title>{{ page_title }}</title> <link rel="stylesheet" href="static/main. css"/> </head> <body> {% block body %} {% endblock %} </body> </html>

middle. html {% extends 'basepage. html' %} {% block body %} <h 2>{{ page_title

middle. html {% extends 'basepage. html' %} {% block body %} <h 2>{{ page_title }}</h 2> <form method='POST' action='/sum'> <table> <p>Sum of two values</p> <tr><td>First Value: </td><input name="first. Value" type="TEXT" width="10"</td></tr> <tr><td>Second Value: </td><input name="second. Value" type="TEXT" width="10"</td></tr> </table> <p><input value="Calculate" type="SUBMIT"></p> </form> {% endblock %}

result. html {% extends "basepage. html" %} {% block body %} <h 2>{{ page_title

result. html {% extends "basepage. html" %} {% block body %} <h 2>{{ page_title }}</h 2> <p>You submitted the following data: </p> <table> <tr><td>First: </td><td>{{ first_value }}</td></tr> <tr><td>Second: </td><td>{{ second_value }}</td></tr> </table> <p>Result</p> <h 3>{{ sum_result }}</h 3> {% endblock %}

hello_plask. py from flask import Flask, render_template, request app=Flask(__name__) @app. route('/') def entry_page()->'html': return

hello_plask. py from flask import Flask, render_template, request app=Flask(__name__) @app. route('/') def entry_page()->'html': return render_template('einstein. html', page_title='Wellcome to Little Einstein Project') @app. route('/sum', methods=['POST']) def sum()->'html': x=int(request. form['first. Value']) y=int(request. form['second. Value']) return render_template('result. html', page_title='Calculation result', sum_result=(x+y), first_value=x, second_value=y, ) app. run(debug=True)