Web-1作业详解
url = 'http://movie.douban.com/top250'
print('url[:0]:{}'.format(url[:0]))
print('url[:1]:{}'.format(url[:1]))
print('url[:2]:{}'.format(url[:2]))
print('url[:3]:{}'.format(url[:3]))
print('url[:5]:{}'.format(url[:5]))
print('url[:7]:{}'.format(url[:7]))
print('url[:100]:{}'.format(url[:100]))
u = url.split('://')[1]
x = url.split('://')[0]
print('u:{}'.format(u))
print('x:{}'.format(x))
url[:0]:
url[:1]:h
url[:2]:ht
url[:3]:htt
url[:5]:http:
url[:7]:http://
url[:100]:http://movie.douban.com/top250
u:movie.douban.com/top250
x:http
url.split('://')[1]以://为分界切分成俩字符串数组,0是第一个,1是第二部分.
def parsed_url(url):
# https://g.cn:1234/hello
protocol = 'http'
if url[:7] == 'http://':
u = url.split('://')[1]
x = url.split('://')[0]
elif url[:8] == 'https://':
protocol = 'https'
u = url.split('://')[1]
else:
# '://' 定位 然后取第一个 / 的位置来切片
u = url
#g.cn:1234/hello
i = u.find('/')
print('i:{}'.format(i))
if i == -1:
host = u
path = '/'
print('host:{}path:{}'.format(u[:i], path))
else:
host = u[:i]
print('u[:i]:{}'.format(u[:i]))
path = u[i:]
print('u[i:]:{}'.format(u[i:]))
# 检查端口
port_dict = {
'http': 80,
'https': 443,
}
# 默认端口
port = port_dict[protocol]
if ':' in host:
h = host.split(':')
host = h[0]
port = int(h[1])
print('host:{} port:{}'.format(host,port))
return protocol, host, port, path
def main():
url = 'g.cn:3000/search'
parsed_url(url)
输出
i:9
u[:i]:g.cn:3000
u[i:]:/search
host:g.cn port:3000
i 等于9, u[:i]等于g.cn:3000这是9个字符.1到9一共9个.
u[i:]等于/search从第九个字符开始不包括9.
测试函数
# 以下 test 开头的函数是单元测试
def test_parsed_url():
"""
parsed_url 函数很容易出错, 所以我们写测试函数来运行看检测是否正确运行
"""
http = 'http'
https = 'https'
host = 'g.cn'
path = '/'
test_items = [
('http://g.cn', (http, host, 80, path)),
('http://g.cn/', (http, host, 80, path)),
('http://g.cn:90', (http, host, 90, path)),
('http://g.cn:90/', (http, host, 90, path)),
#
('https://g.cn', (https, host, 443, path)),
('https://g.cn:233/', (https, host, 233, path)),
]
for t in test_items:
url, expected = t
u = parsed_url(url)
# assert 是一个语句, 名字叫 断言
# 如果断言成功, 条件成立, 则通过测试
# 否则为测试失败, 中断程序报错
e = "parsed_url ERROR, ({}) ({}) ({})".format(url, u, expected)
assert u == expected, e
get函数
- header, body = r.split('\r\n\r\n', 1) 第二个参数 1 ,是只切一次,因为这个\r\n\r\n是头header和体body的分界线.
def parsed_response(r):
header, body = r.split('\r\n\r\n', 1)
h = header.split('\r\n')
status_code = h[0].split()[1]
status_code = int(status_code)
headers = {}
print('h----:{}'.format(h))
print('h[1:]----:{}'.format(h[1:]))
for line in h[1:]:
print('line----:{}'.format(line))
k, v = line.split(': ')
headers[k] = v
return status_code, headers, body
h----:['HTTP/1.1 301 Moved Permanently', 'Date: Fri, 10 Nov 2017 04:49:05 GMT', 'Content-Type: text/html', 'Transfer-Encoding: chunked', 'Connection: close', 'Location: https://movie.douban.com/top250', 'Server: dae', 'X-Content-Type-Options: nosniff']
h[1:]----:['Date: Fri, 10 Nov 2017 04:49:05 GMT', 'Content-Type: text/html', 'Transfer-Encoding: chunked', 'Connection: close', 'Location: https://movie.douban.com/top250', 'Server: dae', 'X-Content-Type-Options: nosniff']
line----:Date: Fri, 10 Nov 2017 04:49:05 GMT
line----:Content-Type: text/html
line----:Transfer-Encoding: chunked
line----:Connection: close
line----:Location: https://movie.douban.com/top250
line----:Server: dae
line----:X-Content-Type-Options: nosniff
- 还是list的操作.先切成list,然后[1:]是不要第一个留其他的.
Web-2-2
server.py
- 请注意代码的格式和规范(PEP8)
类空两行,内部空一行. - if name = 'main':
只在自己函数运行,导入其他函数不运行,因为其他函数不是main了. - dict字典,其他语言中的map.set是key不重复的字典.
两种方式吧.推荐第一种.
config = dict(
host='',
port=3000,
)
confing = {
'host':'',
'port': 3000
}
Web-2-3
*整个程序就是浏览器发送GET&POST,服务器request打印出来的区别.
get 在path发送数据
post 在表单发送数据
原始结果如下.
GET
log ip and request, ('127.0.0.1', 63579)
GET /?author=tanyue&message=dahuaidan HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:3000/msg
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2
Cookie: SID=s%3Aj5EvLdf8fJbNZiOuNigTYhGDKgJkaOTT.geyhCc9aOaLLI%2B4bH0ivUxU8f5aSoWXV7sWWDzQDs1o; optimizelyEndUserId=oeu1509719645050r0.6015629543541059; _ga=GA1.1.1695961780.1490176344; Hm_lvt_dec99185042a5ffec601faced654a817=1509950985,1510121820,1510122239,1510138026; Hm_lpvt_dec99185042a5ffec601faced654a817=1510292868
POST
log ip and request, ('127.0.0.1', 63613)
POST /lqyj HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 31
Cache-Control: max-age=0
Origin: http://localhost:3000
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:3000/msg
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2
Cookie: SID=s%3Aj5EvLdf8fJbNZiOuNigTYhGDKgJkaOTT.geyhCc9aOaLLI%2B4bH0ivUxU8f5aSoWXV7sWWDzQDs1o; optimizelyEndUserId=oeu1509719645050r0.6015629543541059; _ga=GA1.1.1695961780.1490176344; Hm_lvt_dec99185042a5ffec601faced654a817=1509950985,1510121820,1510122239,1510138026; Hm_lpvt_dec99185042a5ffec601faced654a817=1510292938
author=tanyue&message=dahuaidan
authhor就是传的数据
post路径action决定.method决定post&get
网友评论