def pp(x): print(‘as‘) def k(): print (‘k‘) k()//k()必须在def k()同列 才能调用def k()print (pp(2)) //当有2时,pp(x)必须要有参数class Human(object): laugh = ‘hahahaha‘ def show_laugh(self): print self.laugh def laugh_100th(self): for i in range(100): self.show_laugh()//也可以调用 show_laugh()原文:http://www.cnblogs.com/cln-chaoren/p/615878...
单线程 在好些年前的MS-DOS时代,操作系统处理问题都是单任务的,我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。(好吧!我们不纠结在DOS时代是否有听音乐和看影的应用。^_^)from time import ctime,sleepdef music():for i in range(2):print "I was listening to music. %s" %ctime()sleep(1)def move():for i in range(2):print "I was at the movies! %s" %ctime()sleep(5)if __name__ == ‘__main__‘:music()...
Asyncio进行异步IO请求操作:1. @asyncio.coroutine 装饰任务函数2. 函数内配合yield from 和装饰器@asyncio.coroutine 配合使用【固定格式】3. loop = asyncio.get_event_loop()loop.run_until_complete(asyncio.gather(*tasks)) # 接受异步IO的任务并异步执行任务实例一:异步IO: 协程机制 + 回调函数import asyncio@asyncio.coroutine # 装饰任务函数
def func1():print(‘before...func1......‘)# yield from 和装饰器@asy...
python自带的urllib,urllib2可以极方便做http操作,在我们按照http方式提交消息请求后,有可能会看见返回这个错误“403 forbidden",这是请求的网站做了阻止,于是我们需要把自己伪装成模拟器。对此,可以加上headers={‘User-Agent‘:user-agent,‘cookie‘:cookie},注意操作的网站 需要登录账号的话,就需要加上cookie,这两个值可以通过chrome的”开发者工具“查看:把他们加上去,然后在请求值那里再加上:req = urllib2.Reques...
在Python2.x版本里,/usr/bin/lib/python2.x/ 目录下会有 BaseHTTPServer.py, SimpleHTTPServer.py, CGIHTTPServer.py但是在Python3.x里,就没有上面的3个文件,而是合闭到了 /usr/bin/python3.x/http/server.py文件里了。在python2.x里启动CGI服务命令是:$ python -m CGIHTTPServer 8080在python3.x里则是:$ python3.x -m http/server 8080在3.x里,默认的服务RequestHandler是CGIHTTPRequestHandler。原文:http://my.oschina....
用pip安装tornado库:python -m pip install tornado出现问题一: Could not fetch URL https://pypi.org/simple/twisted/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/twisted/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping 解决办法:python -m pip insta...
#!/bin/env python
# -*- coding: UTF-8 -*-
# example: python3 http_status.py https://blog.51cto.com/liuxiaolan/2488619
import requests
import sysdef http_status(arg):try:html = requests.get(arg)code = html.status_codeprint(code)except:print(1)sys.exit(0)if __name__ == "__main__":code = sys.argv[1]http_status(code)原文:https://blog.51cto.com/liuxiaolan/2490018
在自动化脚本中,文件下载是比较常见的操作,一般情况下,我们会将文件放到某个http服务器上,这时,当脚本中需要这个文件时,就需要使用到http下载的功能了最基本的下载功能实现实现最基本的功能,传入文件下载路径和文件本地保存路径,下载到本地def DownloadFile(url,savePath):"""| ##@函数目的: 下载文件| ##@参数说明:url:文件的url路径| ##@参数说明:savePath:文件保存到的位置| ##@返回值:"""try:url = url.strip()sa...
Python命令: python -m SimpleHTTPServer 8000Python代码:import SimpleHTTPServer
import SocketServerclass MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):def handle_one_request(self):print(self.client_address[0])return SimpleHTTPServer.SimpleHTTPRequestHandler.handle_one_request(self)print("Serving local directory")
httpd = SocketServer.TCPServer(("", 8080), MyHandler)while True:httpd.handle_re...
# coding=utf-8
'''
Created on 2014年6月15日@author: Yang
'''import socket
import datetime
# 初始化socket
s = socket.socket()
# 获取主机名, 也可以使用localhost
# host = socket.gethostname()
host = "localhost"
# 默认的http协议端口号
port = 80# 绑定服务器socket的ip和端口号
s.bind((host, port))# 服务器名字/版本号
server_name = "MyServerDemo/0.1"# 缓存时间, 缓存一天
expires = datetime.timedelta(days=1)...
1# getipinfo.py 2def getipinfo(ip):3import requests4 url=‘http://ip.taobao.com/service/getIpInfo.php?ip=‘ 5 r=requests.get(url+ip)6 r1=r.json()7 a={}8 a[‘country‘]=r1[‘data‘][‘country‘]9 a[‘city‘]=r1[‘data‘][‘city‘]
10 a[‘region‘]=r1[‘data‘][‘region‘]
11return a 1import getipinfo2import unittest3import time4class test_getipinfo(unittest.TestCase):5de...
东西很简单,我先废话几句。故事开始于备份公司月报,辛辛苦苦花了两个多小时把公司邮箱里四年的月报用代码过滤筛选之后下载到了虚拟机里,结果虚拟机文件系统崩溃了,重启要求我修复文件系统,修复代价是我要把刚下好的3个g的附件删除一部分,于是我记下了文件数是9090(还好,很好记),开始根据提示clear,之后成功重启,一看附件,删了一半,剩下五千多,我重新跑程序,幸运的是程序跑了半个小时就把附件补齐了,由于程序里下载...
最近正在学习Python中的异步编程,看了一些博客后做了一些小测验:对比asyncio+aiohttp的爬虫和asyncio+aiohttp+concurrent.futures(线程池/进程池)在效率中的差异,注释:在爬虫中我几乎没有使用任何计算性任务,为了探测异步的性能,全部都只是做了网络IO请求,就是说aiohttp把网页get完就程序就done了。结果发现前者的效率比后者还要高。我询问了另外一位博主,(提供代码的博主没回我信息),他说使用concurrent.futures的话因为...
前言 最近一段时间除了忙于工作之外,在业余时,迷上了python,对它的跨平台深深的吸引。经过一段时间的自我学习,了解了其基本的语法,便开始自己着手摆弄起来。主要想把以前对接的接口再实现一次,以便于在做中发现问题,解决问题。只看不做,没有实际的操练,永远都是纸上谈兵。在此过程中遇到了许多问题,通过不断查询资料和请教基本完善了功能。现将自我觉得比较重要的部分拿出来和大家一起探讨一下,也顺便自己对此做个记...
import BaseHTTPServerimport cgi, random, sysMESSAGES = ["That‘s as maybe, it‘s still a frog.","Albatross! Albatross! Albatross!","It‘s Wolfgang Amadeus Mozart.","A pink form from Reading.","Hello people, and welcome to ‘It‘s a Tree.‘""I simply stare at the brick and it goes to sleep.",]class Handler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): if self.path != "/": ...