2440 发表于 2015-12-26 08:56:43

最近郁闷的一个问题:关于Ruby和perl的,有兴趣的看看

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 (Win98; U)',                  
11            
12                     Content_Type =>'form-data',
13                     Content => ,
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

   1require 'rest_client'
   2RestClient.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.
   1Open3.popen3('curl<and your parameters>') do |input, output, error|
   2# do something
   3end




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

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
       #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
   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]
查看完整版本: 最近郁闷的一个问题:关于Ruby和perl的,有兴趣的看看