954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

IP adresses

Ok, Im still really shaky w/useing python with other programs. So here is my question, is it possible to create just a very small program that grabs my IP adress, copys it, and then sends it to certain IM buddys? The reason I am asking this is beacause my friends and I play a multiplayer game but, the game requires me to give out my IP to every singe person who wants to play, not to mention I also use a program called teamspeak that requires them to type my ip. If it can't send it to im buddys is it possible to just have it copy it to the clipboard?

Help apprecaited. :cheesy:

danizzil14
Junior Poster in Training
68 posts since Jul 2005
Reputation Points: 16
Solved Threads: 1
 

It might depend on what OS you are using, but here is one way ...
[php]# socket.getaddrinfo returns a list of tuples
# tuple[4][0] of each list element is the IP address

import socket

addrs = socket.getaddrinfo(socket.gethostname(), None)
for addr in addrs:
print addr[4][0]
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

Wait so all i need to do is use that code and it will copy it to my clip board? Or will it just print it in the DOS box? Oh and I am using XP for Windows. And what is a tuples?

danizzil14
Junior Poster in Training
68 posts since Jul 2005
Reputation Points: 16
Solved Threads: 1
 

This is not very efficient, but you could get your ip using a webbased service

import urllib2
def getip():
    webaddress = "http://www.getip.com"
    page = urllib2.urlopen(webaddress)
    pagetolines = page.readlines()
    for i in pagetolines:
        if i.find('<title>GetIP') != -1:
            ipline = i
            break
    ip = ipline.split()[-1].replace("</title>","")
    return ip
print getip()


I am not sure how to get that on you clipboard.

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

It might depend on what OS you are using, but here is one way ... [php]# socket.getaddrinfo returns a list of tuples # tuple[4][0] of each list element is the IP address

import socket

addrs = socket.getaddrinfo(socket.gethostname(), None) for addr in addrs: print addr[4][0] [/php]

I just tested your code, that just gets the internal ip, would he need his external IP?

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

Good question, I am not familiar with the program called teamspeak.

The socket module brings up both IPs on Windows as long as you are connected, otherwise only the internal IP.

You might have to go to wxPython to handle the ClipBoard.

A tuple is an immutable collection of objects separated by commas, usually enclosed in (). In the socket code just print addrs to see what it looks like.

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

using pywin32 adding to the clipboard is easy.

import win32clipboard, urllib2

# this function just gets the ip from reading page source, and parisng it out
def getip():
    webaddress = "http://www.getip.com"
    page = urllib2.urlopen(webaddress)
    pagetolines = page.readlines()
    for i in pagetolines:
        if i.find('<title>GetIP') != -1:
            ipline = i
            break
    ip = ipline.split()[-1].replace("</title>","")
    return ip

clippedtext = getip()

# this function just places the ip into the clipboard, it takes its parameter from getip
def clippy(text):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(text)
    win32clipboard.CloseClipboard()
    
clippy(clippedtext)
shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

How about Tkinter I've learned basic programming in that but i dont know if i can learn wx Python or Pywin32 but if i have to then i dont mind learning that in place of Tkinter.

danizzil14
Junior Poster in Training
68 posts since Jul 2005
Reputation Points: 16
Solved Threads: 1
 

Good question, I am not familiar with the program called teamspeak.

The socket module brings up both IPs on Windows as long as you are connected, otherwise only the internal IP.

You might have to go to wxPython to handle the ClipBoard.

A tuple is an immutable collection of objects separated by commas, usually enclosed in (). In the socket code just print addrs to see what it looks like.

Oh, and if you goto goteamspeak.com then it might help you get aquainted with the program, all it is, is a live chat program that uses mics and speakers instead of cell phones (or house phones), but beacause of the way it is set up you cant just click on a link to join a server, you need the ip of the server(the computer)

danizzil14
Junior Poster in Training
68 posts since Jul 2005
Reputation Points: 16
Solved Threads: 1
 

Oh and I am reeeeeeeeeeaaaaaaaaaalllllllly shaky with using classes, that is probably the thing I am most having trouble with when it comes to Python and Tkinter.[I just can't fully grasp the consept (ie. don;t understand why self is in there that really bugs me)]

danizzil14
Junior Poster in Training
68 posts since Jul 2005
Reputation Points: 16
Solved Threads: 1
 
Oh and I am reeeeeeeeeeaaaaaaaaaalllllllly shaky with using classes, that is probably the thing I am most having trouble with when it comes to Python and Tkinter.[I just can't fully grasp the consept (ie. don;t understand why self is in there that really bugs me)]

i hear ya, classed give me trouble also :-)

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You