-1

I'm doing pages in html and python (I'm novice in python), I would like to have IP client address, but I don't know if it is possible. I saw it is possible with PHP language.

So, I execute my code in command line (with Linux) like that:

./code.py client_server app_name app_version

infos.py

def main( client_server, app_name, app_version):
  template = open('infoHTML.py').read()
  c = string.Template(template).substitute(
            app_name = app_name,
            app_version = app_version,
            os = user,
            user = login)      
  f = tempfile.NamedTemporaryFile(prefix='/tmp/info.html', mode='w', delete=False)
  f.write(contenu)
  f.close()
  webbrowser.open(f.name)

if __name__ == "__main__":
  client_server = sys.argv[1]
  app_name = sys.argv[2]
  app_version = sys.argv[3]

  user = sys.platform
  sys.argv.append(user)

  login = getpass.getuser()
  sys.argv.append(login)

  main(client_server, app_name, app_version)

I have an html code into python code here: infoHTML.py

<html>
App: ${app_name}<br/><br/>
Version: ${app_version}<br/><br/>
User: ${user}<br/><br/>
<form name="sendData" method="get" action="http://localhost:8000/cgi/display.py">
    Project: <input type="text" name="pro"><br/><br/>
    Number: <input type="text" name="num"/><br/><br/>
    <input type="submit" value="OK"/>
</form>
</body>
</html> 
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
ryu
  • 1
  • 1

1 Answers1

0

It's possible. You need to do it either by rendering the address on the response body or by requesting it with ajax after the response has already been rendered.

It would be hard to give you a code solution without seeing what web server you are using, but here are a couple of pointers for the first approach. To obtain the address, on the server side (python handler):

import socket
ip = socket.gethostbyname(socket.gethostname())

or if you are using something like Google App Engine:

ip = self.request.remote_addr

you should then write the IP to the response. For example, if you are using a templating engine to render your HTML, your HTML can look like something similar to this:

<html>
<script>
var ip = {{ip}}
</script>

and on the python code that renders the template you should do something like that:

htmlContent = template.render(ip=ip)
self.response.write(htmlContent)
orcaman
  • 6,263
  • 8
  • 54
  • 69
  • I have edited my code, I thought to put client_server = socket.gethostname() in substitute and make in html code : action="http:client_server/cgi/scriptGet.py" – ryu Jul 18 '14 at 11:51