The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
SMPT and sending emails in cgi, and random code
Discuss SMPT and sending emails in cgi, and random code in the Python Programming forum on Dev Shed. SMPT and sending emails in cgi, and random code Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 29th, 2003, 06:07 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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
|

September 29th, 2003, 09:32 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

September 29th, 2003, 09:58 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

September 30th, 2003, 05:58 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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
|

October 2nd, 2003, 08:33 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

October 2nd, 2003, 02:25 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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
|

October 2nd, 2003, 04:06 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

October 2nd, 2003, 04:31 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

October 3rd, 2003, 02:26 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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
|

October 3rd, 2003, 06:55 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

October 6th, 2003, 05:49 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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
|

October 6th, 2003, 05:52 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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.
|

October 6th, 2003, 07:02 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 36
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()
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|