#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11

    SMPT and sending emails in cgi, and random code


    Can anyone help me. I've read the python documentation for the module but i still dont quite understand it.

    Also can someone help me with the code to write a 10 character id number containing letters both cap and not and numbers. like this:
    9oPlkj23m7

    I need this to identify users in my cgi program.

    Thanks,
    Conrad
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Yeah i gotta agree with you there, the example in Python's smtplib module is a bit hard to make sence of at first . Anyway here's an simpler example.

    Code:
    #!/usr/bin/env python
    
    import smtplib
    
    #address of mail server to use.
    host = "mail.hotmail.com"
    
    #address tupple as from and to, subject and message.
    address = ("from-address", "to-address")
    subject = ""
    message = ""
    
    #construct mime header. From, To, Subject and the Message.
    message = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % (address[0], address[1], subject, message)
    
    #smtp functions to send the email.
    server = smtplib.SMTP(host)
    server.sendmail(address[0], address[1], message)
    server.quit()
    I think the comments say it all but if you dont understand i'd be happy to explain further . Oh, why does the id have to be letters and numbers, could you not just use the random module to generate a random number id for the user?

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok after a little mess around here's a very small, very simple example of how you could randomly generate a string of 10 random numbers and letters

    Code:
    #!/usr/bin/env python
    
    import random
    
    #char list numbers and letters to be used.
    chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    #print a randonly generated selection of char's from char.
    print ''.join(random.sample(chars, 10))
    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    Thanks a Lot, i just though numbes and letters would be harder to crack for security reasons
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I can see the advantages of that, but i find it very unlikly that even the most persistant cracker would sit there and run though loading a webpage up to 1000000000 times inorder to get a users session i'd which will expire within x time .

    Mmmm and, for that to work you would need to check every number at once because the session id may become active after you have checked that number.. interesting subject .

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    Hey i checked your id code and it doesnt seem to work. It says sample is not part of the module random. I wrote this code though and it seems to work.


    chars = 'thesamethingyouposted'
    x = ''

    for y in range(10):
    x = x + random.choice(chars)

    im still a newb so i dont know if this code is 'right' or not.

    Also i looked at your smpt thingy but i dont have the capabilities to test it out. I was wondering if you need a password to acces the email account. My email is arkamir@softhome.net, host = mail.softhome.net.
    it requires a password when i log on so dont you need one in the program??

    One more thing. My security to logging on is at the creation of an account you will be assigned an id. Then whenever you log on a cookie with you id will be sent to you. Then when you try to post it will check your cookie with the databse id. Is this sound?? I think it prevents people from creating cookies themselves.

    Thanks
    Conrad
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sorry dude, my code works fine with Python 2.3.. Unfortunalty sample() is new so you wont be able to take advantage of it if your using an older version.. What version of Python are you currently using anyway?

    Anyway yeah your code works fine , what you will need to be careful to do is reset x each time you call it (if you call it more than once) because your script keeps adding to x.

    No, no password required, you can use any mailserver you want not just the ones you have an account with which is cool . How do you mean you don't have the capabilities to test it out?

    I've since turned this code into a function which i'll attach to this post..

    Ok now i get ya if it was just numbers then it would be easier than i thought, since the users number is fixed. It may be nice to generate a new id each time the cookie is set and update your database, then even if somone does find out the id it would have changed and so be useless

    Mark.
    Attached Files
    programming language development: www.netytan.com Hula

  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, a couple of more "version friendly" ways to get a random id of 10 numbers an letters

    Code:
    import random
    chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    id = [random.choice(chars) for i in range(10)]
    Code:
    import random
    chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    id = map(lambda x: random.choice(chars), range(10))
    Both of these return a list so you'll still have to join these, either using the string types join method (as in my previous reply) or by importing the string module and using the join function. Just some alternatives for you

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    I have v 2.2 or 2.1 im not sure. Ill take your suggestion and create a new id each time. How do i reset x if I want to call the my id genrator more then once in a script. So if it is called rid()
    x = rid()
    y = rid()
    How woiuld i do it and is there anyway to avoid it??
    I cant test it our sadly since my comp doesnt have internet, my dads comp is kinda screwy since weve been messign around with rawhide and ximian for redhat.
    I'm at the library now.

    btw how does the smpt module work if you dont need a password, cant you just impersonate someone?? that sounds a little scary
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to find out what version of Python your running under you can do something like this (example from the python shell)

    >>> import sys
    >>> sys.version
    '2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)]'

    If you impliment it as a function it doesn't matter, because x will be reset (x = '') every time the function is being called . I get ya, i used to use the library computer.. very anoying!

    The beauty of the mail server, it sends emails its not interested in the who/what/when and where. You could pretend you where somone else if you want lol

    Mark.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    /me shudders at the damage of some people I know impersonating me could do to my rep
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    Also what does MySQldb return if it cant find the value. Like in the codebelow, what if conrad does not exist in the table users, what wouldresult equal?db = MySQL.connect(host='localhost, user='conrad', passwd='python',db='general')cursor = db.cursor()cursor.execute('SELECT conrad FROM users')result = cursor.fetchall()thanks, Conradp.s. sorry about the simplicity of the question but i dont have mysqldbset up right now so i cant test it, but im working on it.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    11
    darned quickpost i only have a 14.4 line at my house so i only use that.
    db = MySQL.connect(host='localhost, user='conrad', passwd='python', db='general')
    cursor = db.cursor()
    cursor.execute('SELECT conrad FROM users')
    result = cursor.fetchall()

IMN logo majestic logo threadwatch logo seochat tools logo