os :python
與操做系統交互的模塊git
1 os.getcwd() 獲取當前工做目錄,即當前python腳本工做的目錄路徑 2 os.chdir("dirname") 改變當前腳本工做目錄;至關於shell下cd 3 os.curdir 返回當前目錄: ('.') 4 os.pardir 獲取當前目錄的父目錄字符串名:('..') 5 os.makedirs('dirname1/dirname2') 可生成多層遞歸目錄 6 os.removedirs('dirname1') 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 7 os.mkdir('dirname') 生成單級目錄;至關於shell中mkdir dirname 8 os.rmdir('dirname') 刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname 9 os.listdir('dirname') 列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印 10 os.remove() 刪除一個文件 11 os.rename("oldname","newname") 重命名文件/目錄 12 os.stat('path/filename') 獲取文件/目錄信息 13 os.sep 輸出操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/" 14 os.linesep 輸出當前平臺使用的行終止符,win下爲"\r\n",Linux下爲"\n" 15 os.pathsep 輸出用於分割文件路徑的字符串,win下爲";", Linux下爲":" 16 os.name 輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix' 17 os.system("bash command") 運行shell命令,直接顯示 18 os.environ 獲取系統環境變量 19 os.path.abspath(path) 返回path規範化的絕對路徑 20 os.path.split(path) # return tuple(head, tail) where tail is everything after the final slash, 即便爲空 21 os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 22 os.path.basename(path) 返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 23 os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False 24 os.path.isabs(path) 若是path是絕對路徑,返回True 25 os.path.isfile(path) 若是path是一個存在的文件,返回True。不然返回False 26 os.path.isdir(path) 若是path是一個存在的目錄,則返回True。不然返回False 27 os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略 28 os.path.getatime(path) 返回path所指向的文件或者目錄的最後存取時間 29 os.path.getmtime(path) 返回path所指向的文件或者目錄的最後修改時間
還有:github
1 l = os.path.split(r'D:\Users\yangxl\PycharmProjects\yangxl\yangxl.py') 2 print(l) # ('D:\\Users\\yangxl\\PycharmProjects\\yangxl', 'yangxl.py') 3 4 l = os.path.splitext(r'D:\Users\yangxl\PycharmProjects\yangxl\yangxl.py') 5 print(l) # ('D:\\Users\\yangxl\\PycharmProjects\\yangxl\\yangxl', '.py') 6 7 for dirpaths, dirnames, filenames in os.walk(r'D:\Users\yangxl\PycharmProjects\yangxl\datasets'): 8 pass
示例,處理文件web
1 now = time.time() # 每次服務器重啓時就會執行這段代碼 2 for abs_path, dirs, files in os.walk(path): 3 for file_name in files: 4 file_path = os.path.join(abs_path, file_name) 5 try: 6 st = os.stat(file_path) 7 # 刪除超過10天的文件 8 if now - st.st_mtime > 3600 * 24 * 10: 9 os.remove(file_path) 10 except Exception, e: 11 print e
sys:shell
與python解釋器交互的模塊json
1 sys.argv 命令行參數List,第一個元素是程序自己路徑 2 sys.exit(n) 退出程序,正常退出時exit(0) 3 sys.version 獲取Python解釋器的版本信息 4 sys.maxint 最大的Int值 5 sys.path 返回模塊的搜索路徑,是個列表,初始化時使用PYTHONPATH環境變量的值 6 sys.platform 返回操做系統平臺名稱,能夠用於跨平臺開發 7 sys.stdout.write('please:') 8 val = sys.stdin.readline()[:-1] 9 10 示例: 11 if sys.platform == 'win32': 12 os.system('dir') 13 else: 14 os.system('ls')
示例:日誌打印api
1 # city.py 2 3 import sys, os 4 5 def print_log(): 6 f = sys._getframe(1) # depth 7 rv = (os.path.normcase(f.f_code.co_filename), f.f_code.co_name, f.f_lineno) # 文件、調用者、調用者在文件中的行數 8 print(rv) 9 10 11 # gogo.py 12 13 from province.city import print_log 14 15 def gogo(): 16 print_log() 17 18 gogo() 19 # ('/home/yangxl/PycharmProjects/meng/province/city.py', 'print_log', 5) 20 # ('/home/yangxl/PycharmProjects/meng/gogo.py', 'gogo', 4)
添加環境變量,瀏覽器
1 sys.path.insert(0, os.path.split(os.path.split(os.path.split(os.path.abspath(__file__))[0])[0])[0]) # 把根目錄添加到環境變量
logging:bash
日誌級別等級:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET。服務器
日誌配置方法:
1 import logging 2 logging.basicConfig(level=logging.DEBUG, 3 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 4 datefmt='%a, %d %b %Y %H:%M:%S', 5 filename='test.log', # 不配置的話,默認輸出到屏幕,二選一 6 filemode='a') 7 8 logging.debug('debug message') 9 logging.info('info message') 10 logging.warning('warning message') 11 logging.error('error message') 12 logging.critical('critical message')
format的格式:
format參數中可能用到的格式化串: %(name)s Logger的名字 %(levelno)s 數字形式的日誌級別 %(levelname)s 文本形式的日誌級別 %(pathname)s 調用日誌輸出函數的模塊的完整路徑名,可能沒有 %(filename)s 調用日誌輸出函數的模塊的文件名 %(module)s 調用日誌輸出函數的模塊名 %(funcName)s 調用日誌輸出函數的函數名 %(lineno)d 調用日誌輸出函數的語句所在的代碼行 %(created)f 當前時間,用UNIX標準的表示時間的浮 點數表示 %(relativeCreated)d 輸出日誌信息時的,自Logger建立以 來的毫秒數 %(asctime)s 字符串形式的當前時間。默認格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒 %(thread)d 線程ID。可能沒有 %(threadName)s 線程名。可能沒有 %(process)d 進程ID。可能沒有 %(message)s用戶輸出的消息
使用logger對象配置日誌:
1 import logging 2 3 logger = logging.getLogger() 4 # 建立一個handler,用於寫入日誌文件 5 fh = logging.FileHandler('test.log') 6 7 # 再建立一個handler,用於輸出到控制檯 8 ch = logging.StreamHandler() 9 10 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 11 12 fh.setFormatter(formatter) 13 ch.setFormatter(formatter) 14 15 logger.addHandler(fh) # logger對象能夠添加多個fh和ch對象 16 logger.addHandler(ch) 17 18 logger.setLevel(logging.DEBUG) # 設置日誌水平,(在打印日誌以前) 19 20 logger.debug('logger debug message') 21 logger.info('logger info message') 22 logger.warning('logger warning message') 23 logger.error('logger error message') 24 logger.critical('logger critical message')
configparser:
生成配置文件:
1 import configparser 2 3 config = configparser.ConfigParser() 4 5 config["DEFAULT"] = {'ServerAliveInterval': '45', 6 'Compression': 'yes', 7 'CompressionLevel': '9'} 8 9 config['bitbucket.org'] = {'User': 'hg'} 10 config['topsecret.server.com'] = {'Host Port': '50022', 'ForwardX11': 'no'} 11 config['DEFAULT']['ForwardX11'] = 'yes' 12 13 with open('example.ini', 'w') as configfile: 14 config.write(configfile)
對配置項進行基本操做:
1 import configparser 2 3 config = configparser.ConfigParser() 4 5 # --------查 6 config.read('example.ini') 7 8 # 沒列出'DEFAULT',它的數據會列在其餘的鍵值對中,看成默認配置 9 print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] 10 11 print('bytebong.com' in config) # False 12 13 print(config['bitbucket.org']['User']) # hg 14 print(config['DEFAULT']['Compression']) # yes 15 16 print(config['topsecret.server.com']['ForwardX11']) # no 17 18 19 for key in config['bitbucket.org']: 20 print(key) 21 22 print(config.options('bitbucket.org')) # ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] 23 print(config.items('bitbucket.org')) # [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] 24 25 # 讀了默認配置 26 print(config.get('bitbucket.org', 'compression')) # yes 27 28 29 # -------刪,改,增(config.write(open('i.cfg', "w"))) 30 config.add_section('yangxl') 31 32 config.remove_section('topsecret.server.com') 33 config.remove_option('bitbucket.org', 'user') 34 35 config.set('bitbucket.org', 'k1', '11111') 36 37 config.write(open('example.ini', "w")) # 覆蓋原文件
time:
1 import time 2 import datetime 3 4 # 時間戳到字符串, local_time --> strftime 5 timestamp = time.time() 6 struct_time = time.localtime(timestamp) 7 strftime = time.strftime('%Y-%m-%d %H:%M:%S', struct_time) # 2019-04-26 11:53:50 8 # print(strftime) 9 10 # 字符串到時間戳, strptime --> mktime 11 struct_time = time.strptime('2019-04-26 11:53:50', '%Y-%m-%d %H:%M:%S') 12 timestamp = time.mktime(struct_time) # 1556250830.0 13 # print(timestamp) 14 15 # 時間戳到datetime 16 dt = datetime.datetime.fromtimestamp(1556250830.0) # 2019-04-26 11:53:50 17 18 # datetime到時間戳, timetuple --> mktime 19 struct_time = dt.timetuple() 20 timestamp = time.mktime(struct_time) # 1556250830.0 21 # print(timestamp) 22 23 # datetime到date 24 dte = dt.date() # 2019-04-26 25 # print(type(dte)) 26 27 # 給定數字生成datetime 28 dt = datetime.datetime(2019, 4, 26, 12, 23, 34) # 2019-04-26 12:23:34 29 30 # 添加timedelta 31 timedelta = datetime.timedelta(3, hours=3, seconds=60) # 3 days, 3:01:00 32 dt = dt + timedelta # 2019-04-29 15:24:34 33 # print(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) # 2019 4 29 15 24 34 34 35 # 獲取當天0點的時間戳 36 SECONDS_ONE_DAY = 3600*24 37 timestamp_day = timestamp // SECONDS_ONE_DAY * SECONDS_ONE_DAY # 1556236800.0 38 # print(timestamp_day) 39 40 # 計算兩個時間戳相差的天數,天數不是按照24小時計算 41 # 分兩步:計算當天0點時間戳, 42 # 0點時間戳到datetime 43 time_1 = 1556250830.0 44 time_2 = 1556451820.0 45 days = (datetime.datetime.fromtimestamp(time_2 // SECONDS_ONE_DAY * SECONDS_ONE_DAY) - datetime.datetime.fromtimestamp(time_1 // SECONDS_ONE_DAY * SECONDS_ONE_DAY)).days 46 # print(days)
print time.strftime('%Y%m%d') # 20190426
time.monotonic() # 這個貌似是從開機到當前的時間戳啊
types:
dir():
1 # coding:utf8 2 3 import sys 4 import types 5 6 print type(sys) == types.ModuleType # 模塊類型 7 8 print dir(sys) 9 print dir('yangxl') 10 11 # 示例:摘自tornado.Application 12 def _load_ui_modules(self, modules): 13 if isinstance(modules, types.ModuleType): 14 self._load_ui_modules(dict((n, getattr(modules, n)) 15 for n in dir(modules))) 16 elif isinstance(modules, list): 17 for m in modules: 18 self._load_ui_modules(m) 19 else: 20 assert isinstance(modules, dict) 21 for name, cls in modules.items(): 22 try: 23 if issubclass(cls, UIModule): 24 self.ui_modules[name] = cls 25 except TypeError: 26 pass
inspect:
1 def ismodule(object): 2 """Return true if the object is a module.""" 3 return isinstance(object, types.ModuleType) 4 5 def isclass(object): 6 """Return true if the object is a class.""" 7 return isinstance(object, type) 8 9 def ismethod(object): 10 """Return true if the object is an instance method.""" 11 return isinstance(object, types.MethodType)
itertools:
1 import itertools 2 3 s = itertools.count() # 這可不是計數器,有參數的哦 4 print s.next() 5 print s.next() 6 print s.next()
hashlib:
一、
1 >>> import hashlib 2 >>> m = hashlib.md5() 3 >>> m.update(b"Nobody inspects") # bytes類型 4 >>> m.update(b" the spammish repetition") 5 >>> m.digest() 6 b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
二、
1 hasher = hashlib.sha1() 2 for part in self._write_buffer: 3 hasher.update(part) 4 return '"%s"' % hasher.hexdigest()
socket:
1 buf = bytearray(self.read_chunk_size) 2 self.socket.recv_into(buf) # A version of recv() that stores its data into a buffer rather than creating a new string.
1 socket.gethostname() # 'yangxl-Lenovo-ideapad-330C-15IKB'
re:
1 re.compile('([\x00-\x7f]+)') # 十六進制, 匹配ASCII表前128個
1 >>> s = '%E7%BE%8A%E5%B0%8F%E7%BE%9A' 2 >>> s 3 '%E7%BE%8A%E5%B0%8F%E7%BE%9A' 4 >>> import re 5 >>> _asciire = re.compile('([\x00-\x7f]+)') 6 >>> _asciire.split(s) 7 ['', '%E7%BE%8A%E5%B0%8F%E7%BE%9A', '']
帶br的,
1 re.findall(br'\*|(?:W/)?"[^"]*"', "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d")
urllib:
1 from urllib import request, parse 2 3 # s = request.urlopen('http://127.0.0.1:9999/login/?name=羊小羚A&age=26') 4 # print(s.read()) 5 6 d = parse.urlencode([ 7 ('name', '羊小羚'), 8 ('age', '26'), 9 ]) 10 data = request.urlopen('http://127.0.0.1:9999/login/', d.encode('utf8')) 11 print(data.read())
collections:
1 >>> from collections import Mapping 2 >>> dis = {'name': 'yangxl', 'age': 26} 3 >>> isinstance(dis, Mapping) # 沒法理解 4 True
collections.queue,
1 self.queue = collections.deque() 2 self.queue.append(key) 3 self.queue.popleft() 4 。。。不少方法呢。。。
collections.namedtuple,
1 SelectorKey = namedtuple('SelectorKey', ['fileobj', 'fd', 'events', 'data']) # 定義 2 key = SelectorKey(1, 2, 3, 4) # 賦值 3 print(key.fd) # 取值 4 不能修改
collections.defaultdict,
1 from collections import defaultdict 2 CLIENT_EXCEPTION_CACHES = defaultdict(int) 3 had_num = CLIENT_EXCEPTION_CACHES[content_md5] + 1 # 默認是0,而後加1 4 CLIENT_EXCEPTION_CACHES[content_md5] += 1 5 # 就是個字典
heapq:
1 heapq.heappush(self._scheduled, timer)
asyncio.base_events.py中的函數call_at(scheduler.start)
resource:
查看系統資源消耗,
1 import resource 2 resource.getrusage(resource.RUSAGE_SELF)[2]
traceback:
1 import traceback 2 tb = traceback.format_exc() # tb = 'NoneType\n'
pickle、cPickle:
1 >>> import pickle 2 >>> d = {'name': 'yangxl', 'age': 18} 3 >>> d 4 {'age': 18, 'name': 'yangxl'} 5 >>> s = pickle.dumps(d) 6 >>> s 7 b'\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x12X\x04\x00\x00\x00nameq\x02X\x06\x00\x00\x00yangxlq\x03u.' 8 >>> type(s) 9 <class 'bytes'> 10 11 12 >>> import cPickle as pickle # 只有py2纔有 13 >>> d = {'name': 'yangxl', 'age': 18} 14 >>> s = pickle.dumps(d) 15 >>> s 16 "(dp1\nS'age'\np2\nI18\nsS'name'\np3\nS'yangxl'\np4\ns." 17 >>> type(s) 18 <type 'str'>
zlib:
1 # py2 2 3 >>> d 4 {'age': 18, 'name': 'yangxl'} 5 >>> zlib.compress(d) 6 Traceback (most recent call last): 7 File "<stdin>", line 1, in <module> 8 TypeError: compress() argument 1 must be string or read-only buffer, not dict 9 >>> 10 >>> zlib.compress('d') 11 'x\x9cK\x01\x00\x00e\x00e' 12 >>> zlib.compress(b'd') 13 'x\x9cK\x01\x00\x00e\x00e' 14 15 16 # py3 17 18 >>> d 19 {'age': 18, 'name': 'yangxl'} 20 >>> zlib.compress(d) 21 Traceback (most recent call last): 22 File "<stdin>", line 1, in <module> 23 TypeError: a bytes-like object is required, not 'dict' 24 >>> 25 >>> zlib.compress('d') 26 Traceback (most recent call last): 27 File "<stdin>", line 1, in <module> 28 TypeError: a bytes-like object is required, not 'str' 29 >>> 30 >>> zlib.compress(b'd') 31 b'x\x9cK\x01\x00\x00e\x00e' 32 33 到了py3中,即便是字符串類型也不行了
weakref:
1 temp = property_class.get(self.uid, server) # 對象 2 setattr(temp, 'weak_user', weakref.proxy(self)) # 弱引用外部傳入user,避免循環引用 3 setattr(self, "_%s" % property_name, temp) # 還有個下劃線
requests模塊
import requests # res = requests.get('http://dig.chouti.com') # 等同於 res = requests.request( url='http://dig.chouti.com', method='get' ) print(res)
url、method、params、data、json、headers、cookies
get方法
import requests
res = requests.request(
url='https://www.sogou.com/web',
method='get',
params={'query':'羊小羚'} #會拼接成https://www.sogou.com/web?query=羊小羚&...
)
print(res.text)
# 'https://www.sogou.com/web?query=羊小羚' #搜狗
# 'https://www.baidu.com/s?wd=羊小羚' #百度
post方法
form_data = {'phone':8615313144407,'password':'123456','oneMonth':1}
res = requests.post(
url = 'http://dig.chouti.com/login',
data = form_data
)
print(res.text) # {"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_48196352125"}}}
data和json
res = requests.post( url = 'http://127.0.0.1:8000/pachong', # data = {'phone':8615313144406,'password':'chouti123456','oneMonth':1}, # content-type: application/x-www-form-urlencoded,數據傳到request.body中而後繼續傳到request.port中。 # request.POST: <QueryDict: {'oneMonth': ['1'], 'phone': ['8615313144406'], 'password': ['chouti123456']}> # request.body: b'oneMonth=1&password=chouti123456&phone=8615313144406' json = json.dumps("{'phone':8615313144406,'password':'chouti123456','oneMonth':1}'") # content-type: application/json,數據只傳到request.body中。 # request.POST: <QueryDict: {}> # request.body: b'"\\"{\'phone\':8615313144406,\'password\':\'chouti123456\',\'oneMonth\':1}\'\\""' ) print(res.text)
headers
res = requests.get( url='https://www.zhihu.com/', headers={ 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36', # 'Referer' : 'https://www.zhihu.com/', # 若是使用瀏覽器能夠正常訪問,可是使用爬蟲就不行,那可能就遇到反爬蟲策略了,須要添加如下兩個參數: # User-Agent,表明使用什麼設備訪問,(僞造瀏覽器) # Referer,從哪裏跳轉過來的,通常狀況下都是從首頁跳轉 }, ) print(res.text) #An internal server error occured.
cookies
res = requests.get( url='https://i.cnblogs.com/Configure.aspx', # headers={ # 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36', # 'Referer' : 'https://www.zhihu.com/', # 若是使用瀏覽器能夠正常訪問,可是使用爬蟲就不行,那可能就遇到反爬蟲策略了,須要添加如下兩個參數: # User-Agent,表明使用什麼設備訪問,(僞造瀏覽器) # Referer,從哪裏跳轉過來的,通常狀況下都是從首頁跳轉 # }, # 當須要登陸才能訪問時,可能須要cookies或session cookies={ '.CNBlogsCookie' : '886F4CA84CC7BC01EF975E3F27EAF9CC3D894702B2EDD7F2A8C373A603E1CECB1013D7B7BA5734628DFE8091863906995C70C0AB1F9A57B4C7F7A47C29286B9D25139D87A5A96B06438933A87E63790AF63AD83A' } ) print(res.text)
def request(method, url, **kwargs): """Constructs and sends a :class:`Request <Request>`. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json data to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``. :param stream: (optional) if ``False``, the response content will be immediately downloaded. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :return: :class:`Response <Response>` object :rtype: requests.Response Usage:: >>> import requests >>> req = requests.request('GET', 'http://httpbin.org/get') <Response [200]> """
實例:
def param_method_url():
# requests.request(method='get', url='http://127.0.0.1:8000/test/')
# requests.request(method='post', url='http://127.0.0.1:8000/test/')
pass
def param_param():
# - 能夠是字典
# - 能夠是字符串
# - 能夠是字節(ascii編碼之內)
# requests.request(method='get',
# url='http://127.0.0.1:8000/test/',
# params={'k1': 'v1', 'k2': '水電費'})
# requests.request(method='get',
# url='http://127.0.0.1:8000/test/',
# params="k1=v1&k2=水電費&k3=v3&k3=vv3")
# requests.request(method='get',
# url='http://127.0.0.1:8000/test/',
# params=bytes("k1=v1&k2=k2&k3=v3&k3=vv3", encoding='utf8'))
# 錯誤
# requests.request(method='get',
# url='http://127.0.0.1:8000/test/',
# params=bytes("k1=v1&k2=水電費&k3=v3&k3=vv3", encoding='utf8'))
pass
def param_data():
# 能夠是字典
# 能夠是字符串
# 能夠是字節
# 能夠是文件對象
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# data={'k1': 'v1', 'k2': '水電費'})
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# data="k1=v1; k2=v2; k3=v3; k3=v4"
# )
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# data="k1=v1;k2=v2;k3=v3;k3=v4",
# headers={'Content-Type': 'application/x-www-form-urlencoded'}
# )
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# data=open('data_file.py', mode='r', encoding='utf-8'), # 文件內容是:k1=v1;k2=v2;k3=v3;k3=v4
# headers={'Content-Type': 'application/x-www-form-urlencoded'}
# )
pass
def param_json():
# 將json中對應的數據進行序列化成一個字符串,json.dumps(...)
# 而後發送到服務器端的body中,而且Content-Type是 {'Content-Type': 'application/json'}
requests.request(method='POST',
url='http://127.0.0.1:8000/test/',
json={'k1': 'v1', 'k2': '水電費'})
def param_headers():
# 發送請求頭到服務器端
requests.request(method='POST',
url='http://127.0.0.1:8000/test/',
json={'k1': 'v1', 'k2': '水電費'},
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
def param_cookies():
# 發送Cookie到服務器端
requests.request(method='POST',
url='http://127.0.0.1:8000/test/',
data={'k1': 'v1', 'k2': 'v2'},
cookies={'cook1': 'value1'},
)
# 也可使用CookieJar(字典形式就是在此基礎上封裝)
from http.cookiejar import CookieJar
from http.cookiejar import Cookie
obj = CookieJar()
obj.set_cookie(Cookie(version=0, name='c1', value='v1', port=None, domain='', path='/', secure=False, expires=None,
discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False,
port_specified=False, domain_specified=False, domain_initial_dot=False, path_specified=False)
)
requests.request(method='POST',
url='http://127.0.0.1:8000/test/',
data={'k1': 'v1', 'k2': 'v2'},
cookies=obj)
def param_files():
# 發送文件
# file_dict = {
# 'f1': open('readme', 'rb')
# }
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# files=file_dict)
# 發送文件,定製文件名
# file_dict = {
# 'f1': ('test.txt', open('readme', 'rb'))
# }
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# files=file_dict)
# 發送文件,定製文件名
# file_dict = {
# 'f1': ('test.txt', "hahsfaksfa9kasdjflaksdjf")
# }
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# files=file_dict)
# 發送文件,定製文件名
# file_dict = {
# 'f1': ('test.txt', "hahsfaksfa9kasdjflaksdjf", 'application/text', {'k1': '0'})
# }
# requests.request(method='POST',
# url='http://127.0.0.1:8000/test/',
# files=file_dict)
pass
def param_auth():
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
ret = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('wupeiqi', 'sdfasdfasdf'))
print(ret.text)
# ret = requests.get('http://192.168.1.1',
# auth=HTTPBasicAuth('admin', 'admin'))
# ret.encoding = 'gbk'
# print(ret.text)
# ret = requests.get('http://httpbin.org/digest-auth/auth/user/pass', auth=HTTPDigestAuth('user', 'pass'))
# print(ret)
#
def param_timeout():
# ret = requests.get('http://google.com/', timeout=1) # 若是隻有一個,表示發送請求的時間。
# print(ret)
# ret = requests.get('http://google.com/', timeout=(5, 1)) # 若是有兩個,第一個表示發送請求的時間,第二個表示讀取返回內容的時間。
# print(ret)
pass
def param_allow_redirects():
ret = requests.get('http://127.0.0.1:8000/test/', allow_redirects=False)
print(ret.text)
def param_proxies():
# proxies = {
# "http": "61.172.249.96:80", # 使用http協議訪問網頁時,使用'61.172.249.96:80'代理。
# "https": "http://61.185.219.126:3128", # 使用https協議訪問網頁時,使用'https://61.185.219.126:3128'代理。
# }
# proxies = {'http://10.20.1.128': 'http://10.10.1.10:5323'} # 訪問'http://10.20.1.128'時,使用'http://10.10.1.10:5323'代理。
# ret = requests.get("http://www.proxy360.cn/Proxy", proxies=proxies) # 代理,讓別人來發送請求
# print(ret.headers)
# from requests.auth import HTTPProxyAuth
#
# proxyDict = {
# 'http': '77.75.105.165',
# 'https': '77.75.105.165'
# }
# auth = HTTPProxyAuth('username', 'mypassword') #使用代理,須要登陸
#
# r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth)
# print(r.text)
pass
def param_stream():
ret = requests.get('http://127.0.0.1:8000/test/', stream=True)
print(ret.content)
ret.close()
# from contextlib import closing
# with closing(requests.get('http://httpbin.org/get', stream=True)) as r:
# # 在此處理響應。
# for i in r.iter_content():
# print(i)
def requests_session():
import requests
session = requests.Session()
### 一、首先登錄任何頁面,獲取cookie
i1 = session.get(url="http://dig.chouti.com/help/service")
### 二、用戶登錄,攜帶上一次的cookie,後臺對cookie中的 gpsd 進行受權
i2 = session.post(
url="http://dig.chouti.com/login",
data={
'phone': "8615131255089",
'password': "xxxxxx",
'oneMonth': ""
}
)
i3 = session.post(
url="http://dig.chouti.com/link/vote?linksId=8589623",
)
print(i3.text)