Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 29th, 2003, 06:07 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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

Reply With Quote
  #2  
Old September 29th, 2003, 09:32 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old September 29th, 2003, 09:58 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #4  
Old September 30th, 2003, 05:58 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Thanks a Lot, i just though numbes and letters would be harder to crack for security reasons

Reply With Quote
  #5  
Old October 2nd, 2003, 08:33 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old October 2nd, 2003, 02:25 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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

Reply With Quote
  #7  
Old October 2nd, 2003, 04:06 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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
File Type: py netytan.py (1.1 KB, 497 views)

Reply With Quote
  #8  
Old October 2nd, 2003, 04:31 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old October 3rd, 2003, 02:26 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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

Reply With Quote
  #10  
Old October 3rd, 2003, 06:55 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old October 6th, 2003, 05:49 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
/me shudders at the damage of some people I know impersonating me could do to my rep

Reply With Quote
  #12  
Old October 6th, 2003, 05:52 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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.

Reply With Quote
  #13  
Old October 6th, 2003, 07:02 PM
Arkamir Arkamir is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 36 Arkamir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > SMPT and sending emails in cgi, and random code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap