【顶2】Python 学习积累

原文参见链接:http://blog.sina.com.cn/s/blog_505bf9af0100wr8l.html

1. Python 学习网址

http://www.cnblogs.com/known/

http://www.erlangsir.com/category/python/page/3/

http://docs.python.org/library/index.html

2. 通过commandline 执行 .py 脚本

首先要将python的安装目录加入到PATH环境变量中。如:
PATH=%PATH%;c:\python27再到你保存py源文件的目录下执行:
<脚本路径> python helloworld.py

3. Python 通过命令行传参数

http://www.cublog.cn/u3/107101/showart_2247117.html

实例:

  1. import sys
  2. if(len(sys.argv)>2):
  3. print "aaaa"
  4. sys.exit(0)
  5. print "Commandline parameter 1: ",sys.argv[1]
  6. print "Commandline parameter 2: ",sys.argv[2]

4. Python 实现socket通讯(TCP)

http://blog.csdn.net/sunboy_2050/article/details/5969480

实例:(在2.7上测试通过)

Server 端代码:

  1. import socket
  2. address1=('127.0.0.1',2011)
  3. s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. s.bind(address1)
  5. s.listen(5)
  6. cs,address = s.accept()
  7. print 'got connected from',address
  8. cs.send('hello I am server,welcome')
  9. while 1:
  10. ra=cs.recv(512)
  11. print ra

Client端代码:

  1. import socket
  2. address=('127.0.0.1',2011)
  3. s=socket.socket()
  4. s.connect(address)
  5. data=s.recv(512)
  6. print 'the data received is/n ',data
  7. s.send('hihi I am client')
  8. while 1:
  9. sInput=raw_input("Enter message and send to server:\n")
  10. s.send('From client: %s'%sInput)

5. Python 发邮件代码:

参考地址: http://justcoding.iteye.com/blog/918933

  1. import os
  2. import smtplib
  3. import mimetypes
  4. from email.MIMEMultipart import MIMEMultipart
  5. from email.MIMEBase import MIMEBase
  6. from email.MIMEText import MIMEText
  7. from email.MIMEAudio import MIMEAudio
  8. from email.MIMEImage import MIMEImage
  9. from email.Encoders import encode_base64
  10. def sendMail(subject, text, *attachmentFilePaths):
  11. gmailUser = 'huichanglee@163.com'
  12. gmailPassword = '******'
  13. recipient = 'cheers.lee@foxmail.com'
  14. msg = MIMEMultipart()
  15. msg['From'] = gmailUser
  16. msg['To'] = recipient
  17. msg['Subject'] = subject
  18. msg.attach(MIMEText(text))
  19. for attachmentFilePath in attachmentFilePaths:
  20. msg.attach(getAttachment(attachmentFilePath))
  21. mailServer = smtplib.SMTP('smtp.163.com', 25)
  22. mailServer.ehlo()
  23. mailServer.starttls()
  24. mailServer.ehlo()
  25. mailServer.login(gmailUser, gmailPassword)
  26. mailServer.sendmail(gmailUser, recipient, msg.as_string())
  27. mailServer.close()
  28. print('Sent email to %s' % recipient)
  29. def getAttachment(attachmentFilePath):
  30. contentType, encoding = mimetypes.guess_type(attachmentFilePath)
  31. if contentType is None or encoding is not None:
  32. contentType = 'application/octet-stream'
  33. mainType, subType = contentType.split('/', 1)
  34. file = open(attachmentFilePath, 'rb')
  35. if mainType == 'text':
  36. attachment = MIMEText(file.read())
  37. elif mainType == 'message':
  38. attachment = email.message_from_file(file)
  39. elif mainType == 'image':
  40. attachment = MIMEImage(file.read(),_subType=subType)
  41. elif mainType == 'audio':
  42. attachment = MIMEAudio(file.read(),_subType=subType)
  43. else:
  44. attachment = MIMEBase(mainType, subType)
  45. attachment.set_payload(file.read())
  46. encode_base64(attachment)
  47. file.close()
  48. attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachmentFilePath))
  49. return attachment
  50. # start to test
  51. sendMail('Hi,Cheers Li', 'Greetings from lihuichang')

6. Python 获取本机计算机名和ip

方法一:

  1. import socket
  2. name=socket.gethostname()
  3. print name
  4. ip_addr=socket.gethostbyname(name)
  5. print ip_addr

方法二:

  1. from socket import socket, SOCK_DGRAM, AF_INET
  2. s = socket(AF_INET, SOCK_DGRAM)
  3. s.connect(('google.com', 0))
  4. print s.getsockname()

7. Pyhton 网络编程之多线程

http://www.cnblogs.com/xiamiwolf/archive/2010/02/21/1670132.html

http://bbs.chinaunix.net/viewthread.php?tid=1434738

http://daxi.me/2009/08/101/

http://www.51testing.com/?uid-752

posted @ 2012-04-26 16:43  念槐聚  阅读(189)  评论(0编辑  收藏  举报