Reputation: 35233
I am new to web designing. I want to create a web page which runs a python script in the background. The script based on the IP address of the client requesting the page will generate some urls which must be embedded in the webpage. What is the best way to do this? Also, how can I find the client's IP address?
Upvotes: 0
Views: 207
Reputation: 684
Here is a basic HttpServer:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Hello %s!' % self.client_address[0])
return
def main():
try:
server = HTTPServer(('', 80), MyHandler)
print 'started http server'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()
You can access this server by simply typing http://localhost/ in your browser. It will print client IP address. Also have a look at list of web frameworks available for python here: http://wiki.python.org/moin/WebFrameworks
Upvotes: 3
Reputation: 2254
You want to take a look at PEP 333, the WebServer Gateway Interface. It's what is generally used for Python scripts to communicate with webservers. You need this in order to run a python webapp.
On top of that you might want to use a framework like Django or Flask. There are numerous WSGI frameworks for Python, it's up to you to choose whichever one suites your needs the best.
As for the IP part of your question, that depends on the framework you're using. I suggest you start by reading about WSGI, and then start reading about WSGI frameworks.
Upvotes: 3