How to get a viewer's IP address with python?

后端 未结 3 1186
抹茶落季
抹茶落季 2020-12-19 21:49

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 thei

相关标签:
3条回答
  • 2020-12-19 22:13

    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

    0 讨论(0)
  • 2020-12-19 22:19

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

    0 讨论(0)
  • 2020-12-19 22:26

    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

    0 讨论(0)
提交回复
热议问题