❶ 如何使用python在區域網上傳送文件
瀏覽器輸入框輸入文件所在主機 IP 。
❷ 如何在 Python 中模擬 post 表單來上傳文件
最近有個將文件上傳到內部web伺服器上的任務,於是參考了網上部分源碼用python寫了這個小程序,代碼如下:
[python]view plain
#coding:utf-8
'''''
Createdon2015.3.19
@author:damofy
'''
importos
importtime
importsys
importurllib2
'''''
filename待上傳的文件
fieldname表單域中的name屬性
'''
defCreateBody(filename,fieldname,strBoundary):
bRet=False
sData=[]
sData.append('--%s'%strBoundary)
#'Content-Disposition:form-data;name="uploadfile";filename="XX-Net-1.3.6.zip"'
sData.append('Content-Disposition:form-data;name="%s";'%fieldname+'filename="%s"'%os.path.basename(filename))
sData.append('Content-Type:%s '%'application/octet-stream')
try:
pFile=open(filename,'rb')
sData.append(pFile.read())
sData.append('--%s-- '%strBoundary)
bRet=True
finally:
pFile.close()
returnbRet,sData
defuploadfile(http_url,filename,fieldname):
ifos.path.exists(filename):
filesize=os.path.getsize(filename)
print('file:'+filename+'is%dbytes!'%filesize)
else:
print('file:'+filename+'isn'texists!')
returnFalse
strBoundary='---------------------------%s'%hex(int(time.time()*1000))
bRet,sBodyData=CreateBody(filename,fieldname,strBoundary)
ifTrue==bRet:
http_body=' '.join(sBodyData)
stReq=urllib2.Request(http_url,http_body)
stReq.add_header('User-Agent','Mozilla/5.0')
stReq.add_header('Content-Length:','%d'%filesize)
stReq.add_header('Content-Type','multipart/form-data;boundary=%s'%strBoundary)
resp=urllib2.urlopen(stReq,timeout=5)
#getresponse
msg=resp.read()
print("Responsecontent: "+msg)
else:
print("CreateBodyfailed!")
returnbRet
if__name__=='__main__':
iflen(sys.argv)>2:
http_url=sys.argv[1]
filename=sys.argv[2]
else:
print('pythonupload.pyhttp://10.20.131.23/upload./test.dat')
sys.exit()
#參數3"uploadfile"是post表單中的name屬性,需要與服務端保持一致
uploadfile(http_url,filename,'uploadfile')
❸ python selenium怎麼自動上傳文件
給你個例子:
ifnotimg_path:
img_path=os.path.abspath("../res/cvd_test.jpg")
else:
img_path=path.abspath(img_path)
#
up_img_input=WebDriverWait(self.driver,10).until(
EC.presence_of_element_located((By.ID,"updli_file"))
)
up_img_input.send_keys(img_path)
send_img_btn=WebDriverWait(self.driver,10).until(
EC.element_to_be_clickable((By.ID,"send-pic-img"))
)
#time.sleep(self.send_interval)
send_img_btn.click()
上面實現的工作就是,找到上傳組件的那個input元素,然後把你要上傳的文件的路徑發送給ta
❹ python能直接通過url上傳文件嗎
可以這樣寫:
urls = sel.xpath('//li[@class="next_article"]/a/@href').extract()
for url in urls:
url = "http://blog.csdn.net" + url
yield Request(url, callback=self.parse)
❺ 如何用python實現上傳本地指定文件夾下文件(文件名稱均為數字,以數字順序上傳)到遠端指定路徑
這要看你的本地與遠端允許哪些通信方式(協議)
❻ 怎麼用http上傳一個文件到伺服器 python
首先,標准HTTP協議對上傳文件等表單的定義在這里:wwwietforg/rfc/rfc1867txt 大概數據包格式如下:
單文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
多文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y
--BbC04y
Content-disposition: attachment; filename="file1.txt"
其次,python上傳文件的幾種方法:
1 自己封裝HTTP的POST數據包:http//stackoverflowcom/questions/680305/using-multipartposthandler-to-post-form-data-with-python
import httplibimport mimetypesdef post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = httplib.HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() def encode_multipart_formdata(fields, files): LIMIT = '----------lImIt_of_THE_fIle_eW_$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + LIMIT) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files:
❼ Python的文件上傳
Python中使用GET方法實現上傳文件,下面就是用Get上傳文件的例子,client用來發Get請求,server用來收請求。
請求端代碼:
importrequests#需要安裝requests
withopen('test.txt','rb')asf:
requests.get('http://伺服器IP地址:埠',data=f)
服務端代碼:
varhttp=require('http');
varfs=require('fs');
varserver=http.createServer(function(req,res){
//console.log(req);
varrecData="";
req.on('data',function(data){
recData+=data;
})
req.on('end',function(data){
recData+=data;
fs.writeFile('recData.txt',recData,function(err){
console.log('filereceived');
})
})
res.end('hello');
})
server.listen(埠);