import zipfile
import os
import re
import time
if __name__ == '__main__':
import socket
sock = socket.socket()
sock.bind(('', 9066))
sock.listen(5)
filedownadd = 'C:\Python27\chapter2\\'
filedown = os.listdir(filedownadd)
download = []
buf = []
funinclude = ''
while True:
filedown=[]
filedown = os.listdir(filedownadd)
funinclude = 'You can get :\n'+'\n'.join(filedown)+'''
Input 'ask' can know what you can do
Input 'get xxx (file name,just one)'
Input 'need' then you write want you need(the endline must be '#')
Input 'talk' then you can talk with us
Input 'end' then over'''
httphead = '''
Connection: close
Date:'''
httph1=' GMT'+'''
Sever: /
Content-Type: text/html
'''
connection,address = sock.accept()
try:
#connection.settimeout(5)
buf=''
buf = connection.recv(512)
comhead=[]
## 尝试多文件的情况
#if len(download)==0 and buf != '#':
gethead = buf.split('\n')
comhead = [] # if not ,the one will be added
comhead = gethead[0].split(' ')
if len(download)==0 and buf == '#':
buf =''
connection.send('All file is end')
connection.close()
continue
#####
if buf == 'ask':
connection.send(funinclude)
connection.close()
continue
#else:
# connection.send('please go out!')
if buf == 'need':
print buf
f=open('request01.txt','ab')
f.write('\n'+address[0]+':\n')
#f.write(' :\n')
f.write(connection.recv(1024))
f.close()
print 'need is end'
connection.close()
continue
print buf
print comhead
if comhead[0].lower() != 'get' :#and len(download)==0:
comhead=[]
print 'not standard command!'
connection.send('Not standard command!')
connection.close()
continue
if comhead[len(comhead)-1].lower().startswith('http'):
h = open('httprequest.txt','w')
h.write(buf)
h.close()
if comhead[1]=='/':
print 'sent inde0.html'
try:
f=open('index0.html','rb')
connection.send('HTTP /1.1 200 OK'+httphead+time.ctime()+httph1)
while True:
line = f.readline()
if len(line)==0:
break
connection.send(line)
f.close()
except:
connection.send(open('notfound.html','rb').read())
print 'finish index.html'
if comhead[1]!='/':
connection.send(open('notfound.html','rb').read())
connection.close() #持续连接
continue
if len(comhead)!=2:
comhead=[]
connection.send('Please use right call,like get filename(just one)')
connection.close()
continue
if len(download)==0:
download = comhead[1:]
for i in range (0,1):
#connection,address = sock.accept()
flag = True
for j in range(0,len(filedown)):
if download[0]==filedown[j]:
flag = False
f=open(filedownadd+filedown[j],'rb')
connection.send(download[0])
print download[0]+' sent out'
while True:
line = f.read(512)
if len(line)==0:
break
connection.send(line)
f.close()
#connection.send('thisfileisend')
connection.send('')
print download[0]+' is end'
if flag:
connection.send(download[0]+' not find')
#connection.close()
download=download[1:]
#connection.send('All file is end')
print 'All file is end'+':'
print download
## try
except socket.timeout:
print 'time out'
connection.close()
##while
print 'while out'
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
网络实验源代码.rar (5个子文件)
网络实验源代码
test2
eTki_Semail.py 3KB
test3
eUPDSer.py 451B
eUPDCli.py 3KB
test1
httpSever.py 5KB
test4
webSever.py 4KB
共 5 条
- 1
资源评论
- wuzhixi49352013-06-10没看见原代码啊
- 匆匆wqq2014-10-20写的很详细,但是语言不常用,如果能使用Java就好。(无源代码)
- mydykitty2017-07-10很详细,还不错
openjiaohu
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功