Hallo everyone at DaniWeb :)

A made a mail script, that i need to somehow break, to close the connection to the gmail server securely. Can someone give me a hint, what to do for that.

import smtplib
import urllib2
import time

def sendIP():
    # fetch pub ip
    pub_ip = urllib2.urlopen("http://whatismyip.com/automation/n09230945.asp").read()

    msg = """Subject:Server ip!

    My ip today is: """ + pub_ip    

    # Send mail
    server.sendmail(fromaddr, toaddrs, msg)  

    print "The Master Is Informed"

# Credentials
fromaddr = '***@gmail.com'  
toaddrs  = '***@gmail.com'
username = '***'
password = '***'

# Start connection 
server = smtplib.SMTP('smtp.gmail.com:587')  

# This is the loop i need to break. I know there will be another problem with the sleep() :
while 1:

print "Connection Closed.."
commented: Thanks for the idea of email containing server IP. +3

In which case you need to brake? That is infinite loop.

I would do server.quit() in sendIP(), I quess.

I'm not sure why you need an infinite loop but maybe you want to wrap the sendIP() in a try block. The finally block will quit the server whether or not your sendIP() succeeds or causes an exception.

#!/usr/bin/env python
# This is the loop i need to break. I know there will be another problem with the sleep() :
while 1:
    finally: #The following statements run whether or not an exception occurs
        print "Connection Closed.."

The reason for the infinite loop, is just that i have dynamic ip, and whant to update the ip (all the time). I guess the best would be to have the server.quit() in the sendIP(), but that is kind of slow, and for the sake of not reloging in all the time.

I was just thinking something like this:

while 'not pressing any keys':

I'm not even sure if thats posible, because you would have to cancel the sleep().

You could remember what IP was before and in case of change Email the changed IP.
If your program is daemon, then it should just continue.

You should consider something to this (fill in the time variables)

from time import time
from urllib2 import urlopen
pub_ip = urlopen("http://whatismyip.com/automation/n09230945.asp").read
ipnow='' ## always send in beginning
while time()<time_to_guit:
    ipbefore,ipnow = ipnow, pub_ip()
    if ipnow != ipbefore:

Lot better concept than mine, then i don't risk, not having the new IP for 7 hours. ^^ And still reduce the program to send one time every 24 hours.

Thanks for the quick help :)