如何使用python获取查看者的IP地址? [英] How to get a viewer's IP address with python?

查看:90
本文介绍了如何使用python获取查看者的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是用 python 文件做一个基本的论坛,允许用户登录和/或创建一个帐户.我想我会做多一点,让每个用户使用他们的 IP 地址登录,然后能够使用从他们的 IP 地址生成的唯一 ID 发帖,就像大多数图像板一样.

Just doing a basic forum with a python file allowing the user to login and/or create an account. Thought I would do a little bit more and have each user logged in with their IP address and then be able to post with a unique ID generated from their IP address, like most imageboards.

我可以很容易地编写代码,但我无法找到如何在不使用 Flask 的情况下获取用户的 IP 地址.

I can write the code pretty easily, but I am having trouble finding how to get the user's IP address without using Flask.

这是我刚刚检查的代码,它似乎正在运行

this is the code i just checked, and it seems to be working

    #!/usr/bin/python
import md5, cgi,cgitb,codecs
def Maker():
    form=cgi.FieldStorage()#for use later
    a=form.keys()#for use later
    page = 'Content-type: text/html\n\n'
    page +='<html><head><title>Test Form</title></head>\n'
    from socket import gethostname, gethostbyname 
    ip = gethostbyname(gethostname())
    page+= ip
    return page
print Maker()

该代码不起作用,仍在寻求帮助

that code isnt working, still looking for help

推荐答案

听起来您将此作为 CGI 脚本运行.用户的 IP 地址将在 REMOTE_ADDR 环境变量中,os.environ["REMOTE_ADDR"].

It sounds like you're running this as a CGI script. The user's IP address will be in the REMOTE_ADDR environment variable, os.environ["REMOTE_ADDR"].

这篇关于如何使用python获取查看者的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆