六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 47|回复: 0

python网络编程1

[复制链接]

升级  1.33%

58

主题

58

主题

58

主题

举人

Rank: 3Rank: 3

积分
204
 楼主| 发表于 2013-1-15 02:50:02 | 显示全部楼层 |阅读模式
python的sendall和c的send对比



python的文件对象:只要实现了文件对象必须的几个方法,就可以当作“文件”一样操作,duck type,有文件对象,也有file-like ojbect,后者并不一定有文件对象的全部方法和属性。
File Objects
====================
File objects are implemented using C's stdio package.
1, can be created with the built-in open() function
2, returned by some methods,such as os.popen(),os.fdopen(),makefile() method of socket objects
Temporary fils can be created using the tempfile module
>>> from tempfile import *
>>> f = NamedTemporaryFile(delete=False)
>>> f.name
'/tmp/tmpV3sfCh'
>>> f.write('hello\n')
>>> f.read()
''
>>> f.close()
>>> f.name
'/tmp/tmpV3sfCh'
>>> import os
>>> os.unlink(f.name)
>>> os.path.exists(f.name)
False


copying,moving... can be achieved with shutil module

Exception:IOError
Method:
close() operation which requires that the file be open will rais a ValueError after the file has been closed.
flush() C:fflush()
fileno() Return the integer "file descriptor" that is used by the underlying implementations to req I/O
isatty() tty(like) device?
next() Ex:StopIteration when EOF is hit. has a read-ahead buf
read([size])  C:fread
readline([size]) C:fgets .unlike fgets,the returned sring contains null character('\0') if they occurred in the input
readlines([sizehint])
xreadlines()   returns the same thing as iter(f)
should for line in file instead.
seek(offset[,whence]) C:fseek
f.seek(2, os.SEEK_CUR)
f.seek(-3, os.SEEK_END)
tell()   C:ftell
truncate
write(str)
writelines(sequence) eg:list of strings
closed()
encoding if encoding is None,in which case the file uses the system default encoding for converting Unicode strings.
file.errors  unicode error handler used along with the encoding
mode
name
newlines
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表