设为首页 收藏本站
查看: 1056|回复: 0

[经验分享] 最近郁闷的一个问题:关于Ruby和perl的,有兴趣的看看

[复制链接]

尚未签到

发表于 2015-12-26 08:56:43 | 显示全部楼层 |阅读模式
1 #!/usr/bin/perl -w
2 use LWP;
3 use vars '@ISA';
4 @ISA = 'LWP::UserAgent';
5
6 sub get_file{
7     my $url = 'https//www.google.com';
8     my $agent = __PACKAGE__->new;
9     $response = $agent -> request(POST $url,
10                     'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',                    
11            
12                     Content_Type =>'form-data',
13                     Content => [  fileName => [$fname],
14                           waspaction=>"FILEUPLOAD",
15                           swrFile=>[$fname]
16                                         ]
17                 );
18     #$response->is_success or die "$url: ", $response->message, "\n";
19     my $content = $response->content;
20 }
21
22 这段代码,我用ruby改写,如下:
23 def get_rac
24     url = URI.parse('https:/www.google.com');
25     #agent = __PACKAGE__.new;
26     
27      req = Net::HTTP::Post.new(url.path)
      req.basic_auth $uid, $pwd
28     req.set_form_data({:fileName => [$fname],:waspaction=>"FILEUPLOAD",:swrFile=>
29 [$fname]}, ';')
30     
31   
32     res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
33
34     content = res;
35     puts res.body
36 end
37 报错:end of file reached
38     C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in `sysread'
39     C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in `rbuf_fill'
40     C:/Ruby/lib/ruby/1.8/timeout.rb:62:in `timeout'
41     C:/Ruby/lib/ruby/1.8/timeout.rb:93:in `timeout'
42     C:/Ruby/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
43     C:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
44     C:/Ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
45     C:/Ruby/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
46     C:/Ruby/lib/ruby/1.8/net/http.rb:2009:in `read_new'
47     C:/Ruby/lib/ruby/1.8/net/http.rb:1050:in `request'
48     C:/WORKSPACE/get_rac.rb:27:in `get_rac'
49     C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
50     C:/WORKSPACE/get_rac.rb:27:in `get_rac'
51     C:/WORKSPACE/get_rac.rb:39
52 C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in `sysread': end of file reached (EOFError)
53     from C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in `rbuf_fill'
54     from C:/Ruby/lib/ruby/1.8/timeout.rb:62:in `timeout'
55     from C:/Ruby/lib/ruby/1.8/timeout.rb:93:in `timeout'
56     from C:/Ruby/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
57     from C:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
58     from C:/Ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
59     from C:/Ruby/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
60     from C:/Ruby/lib/ruby/1.8/net/http.rb:2009:in `read_new'
61     from C:/Ruby/lib/ruby/1.8/net/http.rb:1050:in `request'
62     from C:/WORKSPACE/get_rac.rb:27:in `get_rac'
63     from C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
64     from C:/WORKSPACE/get_rac.rb:27:in `get_rac'
65     from C:/WORKSPACE/get_rac.rb:39
66 不知道是什么问题。
现在貌似有点头绪了,问题就出在:
req.set_form_data({:fileName => [$fname],:waspaction=>"FILEUPLOAD",:swrFile=>
[$fname]}, ';')
这里。ruby中的意义与perl是不同的,这个地方;


  How to perform a file upload (multipart post) with Ruby

  

      Ruby posted about 1 year ago by christian
  
  

  You have at least 5 options:


  • Stanislav Vitvitskiy’s solution
  • RestClient

   1  require 'rest_client'
   2  RestClient.post 'http://localhost:3000/foo', fields_hash.merge(:file => File.new('/path/to/file'))


  • The curb gem
  • The multipart-post Net:HTTP extension
  • Calling curl from Ruby with, for example, Open3.
       1  Open3.popen3('curl  <and your parameters>') do |input, output, error|
       2  # do something
       3  end

  
  

      Tagged post, multipart, curl, ruby, restclient, upload
我用了第一种方法,奇怪的是发到一半,会有报错:

DSC0000.gif DSC0001.gif Code
class Multipart  
   def initialize( file_names )  
     @file_names = file_names  
   end  
   def post(url,user,password)  
     boundary = '---------------------------7d9b914b082e'  
     parts = []  
     streams = []
     @file_names.each do |param_name, filepath|  
       pos = filepath.rindex('/')
       filename = filepath[pos + 1, filepath.length - pos]
       #puts param_name.to_s
       #puts filepath
       parts << StringPart.new("--" + boundary + "\r\n" +  
       "Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +  
       "Content-Type: text/plain\r\n\r\n")
       stream = File.open(filepath, "rb")  
       streams << stream  
       parts << StreamPart.new(stream, File.size(filepath))  
     end  
     parts << StringPart.new("\r\n--" + boundary + "--\r\n" )  
     post_stream = MultipartStream.new( parts )  
     #url = URI.parse( to_url )
     
     http = Net::HTTP.new(url.host, url.port)   
     http.use_ssl = true
     req = Net::HTTP::Post.new(url.path)
     req.basic_auth "admin","admin"  
     req.content_length = post_stream.size  
     req.content_type = 'multipart/form-data; boundary=' + boundary  
     req.body_stream = post_stream
     res = Net::HTTP.new(url.host, url.port).start{|http| http.request(req)}  
     streams.each do |stream|  
       stream.close();  
     end  
     res  
   end  
end  
class StreamPart  
   def initialize(stream, size)  
     @stream, @size = stream, size  
   end  
   def size  
     @size  
   end  
   def read(offset, how_much)  
     @stream.read(how_much)  
   end  
end  
class StringPart  
   def initialize(str)  
     @str = str  
   end  
   def size  
     @str.length  
   end  
   def read(offset, how_much)  
     @str[offset, how_much]  
   end  
end
An established connection was aborted by the software in your host machine.
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write0'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:151:in `write'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:166:in `writing'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:150:in `write'
C:/Ruby/lib/ruby/1.8/net/http.rb:1557:in `send_request_with_body_stream'
C:/Ruby/lib/ruby/1.8/net/http.rb:1527:in `exec'
C:/Ruby/lib/ruby/1.8/net/http.rb:1048:in `request'
C:/WORKSPACE/get_rac.rb:48:in `post'
C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
C:/WORKSPACE/get_rac.rb:48:in `post'
C:/WORKSPACE/get_rac.rb:137:in `get_rac'
C:/WORKSPACE/get_rac.rb:167
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write': An established connection was aborted by the software in your host machine. (Errno::ECONNABORTED)
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write0'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:151:in `write'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:166:in `writing'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:150:in `write'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1557:in `send_request_with_body_stream'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1527:in `exec'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1048:in `request'
from C:/WORKSPACE/get_rac.rb:48:in `post'
from C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
from C:/WORKSPACE/get_rac.rb:48:in `post'
from C:/WORKSPACE/get_rac.rb:137:in `get_rac'
from C:/WORKSPACE/get_rac.rb:167

截包分析:发小点的文件是可以发全的。大一点的文件发了一半就不能继续上传文件了。实在找不到错误的地方。是
不是少设置了什么参数

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156408-1-1.html 上篇帖子: ---perl的常用CPAN---from web 下篇帖子: perl文件句柄的传递
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表