Python 学习积累《一》

配置Python,Eclipse 环境:

http://www.qrong.com/archives/513

TypeError: 'module' object is not callable 原因分析

Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的限定,而后者不要。

参考文章:http://blog.csdn.net/kenkywu/article/details/6743520

IntelliJ IDEA手工安装插件方法:

参考文章:http://little-bill.iteye.com/blog/1354518

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 读写 csv 文件

import csv
csvFile = file(r'C:\Python27\test\writeTest.csv','wb')
writer = csv.writer(csvFile)
writer.writerow(['Head1','head2','head3'])
lines = [range(3) for i in range(4)]
try: 
    for line in lines:
        writer.writerow(line)
except:
    IOError
finally:
    csvFile.close()

参考文章:

http://python365.blogbus.com/logs/79153374.html

http://www.pythonclub.org/python-files/csv

4. 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]

5. 获取函数名

[python]  view plain copy
  1. def test_fn():  
  2.     pass  

如上面这个函数,想动态获取该函数名"test_fn",如果直接写函数名字符串就买啥意思,且看如下:

 

[python]  view plain copy
  1. import sys 
  2. def test_fn():   
  3.     fn_name (lambda:sys._getframe(1).f_code.co_name)()  
        return 
    fn_name 
  4. print  test_fn

6. Python 发http请求

http://www.cnblogs.com/lwgdream/articles/2486157.html 

http://blog.csdn.net/five3/article/details/7079140

  • import httplib
  • conn = httplib.HTTPConnection("baidu.com")
  • conn.request("GET", "/index.html")
  • r1 = conn.getresponse()
  • print r1.status, r1.reason
  • resultContent = r1.read()
  • print resultContent

7. Python 中 安装 pyExcelerator

方法一:

直接将pyexcelerator-0.6.4.1这个文件夹里的pyExcelerator文件夹复制到

C:\Python26\Lib\site-packages文件夹下就可以使用

方法二:

进入pyExcelerator中 setup.py 所在目录:
通过命令 python setup.py install 进行安装 

8. Python 读写 Excel 操作

http://mj4d.iteye.com/blog/1395631

9. Python 输出日期

import time
print time.strftime('%Y-%m-%d',time.localtime(time.time()))

10.Python 获取当前脚本路径和目录
import os
print os.path.realpath(__file__)
print os.path.abspath(os.curdir)

11. 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)

12. 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')

12. 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()

13. 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-75241-action-viewspace-itemid-98995

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值