4

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.

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

EDIT: 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()

EDIT: that code isnt working, still looking for help

Konrad
  • 55
  • 1
  • 1
  • 5

3 Answers3

7

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"].

axblount
  • 2,639
  • 23
  • 27
  • Do forums generally work as CGI scripts? sounds like a web application to me. OP definitely needs to add more detail to his question though! – Russ Clarke May 15 '13 at 22:10
  • I based my assumption entirely on "Just doing a basic forum with a python file". Your guess is as good as mine! – axblount May 15 '13 at 22:13
  • Updated comment in original post. – Konrad May 15 '13 at 22:21
  • Aye, Sorry axblount; I didn't mean to sound incredulous - it was a genuine question! I daresay you could implement a forum as a series of CGI's; I daresay someone actually has ... For me? life's too short! – Russ Clarke May 15 '13 at 22:32
5

I think this might work

from socket import gethostname, gethostbyname 
ip = gethostbyname(gethostname()) 

or If you are using CGI you can get it from the REMOTE_ADDR environmental variable

rcbevans
  • 7,101
  • 4
  • 30
  • 46
0

Are you assuming each user has a unique IP address? If so, this is not a good or safe way to generate a UserID. In any online community, there will always be some users accessing your site via gateways or proxy servers, so some users will have the same remote IP address.

You could append some random number or timestamp to the IP address, or just use a datetimestamp. But why not just use the user's email address?

HTH