* protocol.rb,smtp.rb,pop.rb,http.rb: modify document.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
37b3f674a7
commit
cf37626401
@ -27,7 +27,7 @@ For details of HTTP, refer [RFC2616]
|
|||||||
=== Getting Document From Server
|
=== Getting Document From Server
|
||||||
|
|
||||||
Be care to ',' (comma) putted after "response".
|
Be care to ',' (comma) putted after "response".
|
||||||
This is required for feature compatibility.
|
This is required for compatibility.
|
||||||
|
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
Net::HTTP.start( 'some.www.server', 80 ) {|http|
|
Net::HTTP.start( 'some.www.server', 80 ) {|http|
|
||||||
@ -73,13 +73,16 @@ there's no need to change code if there's proxy or not.
|
|||||||
Net::HTTP.version_1_1
|
Net::HTTP.version_1_1
|
||||||
|
|
||||||
host = 'www.ruby-lang.org'
|
host = 'www.ruby-lang.org'
|
||||||
|
path = '/'
|
||||||
begin
|
begin
|
||||||
Net::HTTP.start( host, 80 ) {|http|
|
Net::HTTP.start( host, 80 ) {|http|
|
||||||
response , = http.get('/')
|
response , = http.get(path)
|
||||||
|
print response.body
|
||||||
}
|
}
|
||||||
rescue Net::ProtoRetriableError => err
|
rescue Net::ProtoRetriableError => err
|
||||||
if m = %r<http:([^/]+)>.match( err.response['location'] ) then
|
if m = %r<http://([^/]+)>.match( err.response['location'] ) then
|
||||||
host = m[1].strip
|
host = m[1].strip
|
||||||
|
path = m.post_match
|
||||||
retry
|
retry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -131,8 +134,7 @@ Yes, this is not thread-safe.
|
|||||||
|
|
||||||
: new( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
|
: new( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
|
||||||
creates a new Net::HTTP object.
|
creates a new Net::HTTP object.
|
||||||
If proxy_addr is given, this method is equals to
|
If proxy_addr is given, creates an Net::HTTP object with proxy support.
|
||||||
Net::HTTP::Proxy(proxy_addr,proxy_port).
|
|
||||||
|
|
||||||
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
|
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil )
|
||||||
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
|
: start( address = 'localhost', port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
|
||||||
@ -145,15 +147,14 @@ Yes, this is not thread-safe.
|
|||||||
return value is a String.
|
return value is a String.
|
||||||
|
|
||||||
: get_print( address, path, port = 80 )
|
: get_print( address, path, port = 80 )
|
||||||
gets entity body from path and print it.
|
gets entity body from path and output it to $stdout.
|
||||||
return value is an entity body (a String).
|
|
||||||
|
|
||||||
: Proxy( address, port = 80 )
|
: Proxy( address, port = 80 )
|
||||||
creates a HTTP proxy class.
|
creates a HTTP proxy class.
|
||||||
Arguments are address/port of proxy host.
|
Arguments are address/port of proxy host.
|
||||||
You can replace HTTP class by this proxy class.
|
You can replace HTTP class with created proxy class.
|
||||||
|
|
||||||
If ADDRESS is nil, this method returns self (Net::HTTP class).
|
If ADDRESS is nil, this method returns self (Net::HTTP).
|
||||||
|
|
||||||
# example
|
# example
|
||||||
proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
|
proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
|
||||||
@ -168,7 +169,7 @@ Yes, this is not thread-safe.
|
|||||||
If self is a class which was created by HTTP::Proxy(), true.
|
If self is a class which was created by HTTP::Proxy(), true.
|
||||||
|
|
||||||
: port
|
: port
|
||||||
HTTP default port (80).
|
default HTTP port (80).
|
||||||
|
|
||||||
=== Instance Methods
|
=== Instance Methods
|
||||||
|
|
||||||
@ -208,30 +209,32 @@ Yes, this is not thread-safe.
|
|||||||
true if self is a HTTP proxy class
|
true if self is a HTTP proxy class
|
||||||
|
|
||||||
: proxy_address
|
: proxy_address
|
||||||
address of proxy host. If self is not a proxy, nil.
|
address of proxy host. If self does not use a proxy, nil.
|
||||||
|
|
||||||
: proxy_port
|
: proxy_port
|
||||||
port number of proxy host. If self is not a proxy, nil.
|
port number of proxy host. If self does not use a proxy, nil.
|
||||||
|
|
||||||
: get( path, header = nil, dest = '' )
|
: get( path, header = nil, dest = '' )
|
||||||
: get( path, header = nil ) {|str| .... }
|
: get( path, header = nil ) {|str| .... }
|
||||||
gets data from "path" on connecting host.
|
gets data from PATH on the connecting host.
|
||||||
"header" must be a Hash like { 'Accept' => '*/*', ... }.
|
HEADER must be a Hash like { 'Accept' => '*/*', ... }.
|
||||||
Response body is written into "dest" by using "<<" method.
|
Response body is written into DEST by using "<<" method.
|
||||||
|
|
||||||
This method returns Net::HTTPResponse object.
|
This method returns Net::HTTPResponse object.
|
||||||
|
|
||||||
If called with block, give a part String of entity body.
|
If called with block, gives entity body little by little
|
||||||
|
to the block (as String).
|
||||||
|
|
||||||
In version 1.1, this method might raises exception for also
|
In version 1.1, this method might raises exception for also
|
||||||
3xx (redirect). On the case you can get response object by
|
3xx (redirect). On the case you can get a HTTPResponse object
|
||||||
err.response.
|
by "anException.response".
|
||||||
|
|
||||||
In version 1.2, this method never raises exception.
|
In version 1.2, this method never raises exception.
|
||||||
|
|
||||||
# version 1.1 (Ruby 1.6)
|
# version 1.1 (bundled with Ruby 1.6)
|
||||||
response, body = http.get( '/index.html' )
|
response, body = http.get( '/index.html' )
|
||||||
|
|
||||||
# version 1.2 (Ruby 1.7 or later)
|
# version 1.2 (bundled with Ruby 1.7 or later)
|
||||||
response = http.get( '/index.html' )
|
response = http.get( '/index.html' )
|
||||||
|
|
||||||
# compatible in both version
|
# compatible in both version
|
||||||
@ -244,16 +247,20 @@ Yes, this is not thread-safe.
|
|||||||
f.write str
|
f.write str
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
# some effect
|
# same effect
|
||||||
File.open( 'save.txt', 'w' ) {|f|
|
File.open( 'save.txt', 'w' ) {|f|
|
||||||
http.get '/~foo/', nil, f
|
http.get '/~foo/', nil, f
|
||||||
}
|
}
|
||||||
|
|
||||||
: head( path, header = nil )
|
: head( path, header = nil )
|
||||||
gets only header from "path" on connecting host.
|
gets only header from PATH on the connecting host.
|
||||||
"header" is a Hash like { 'Accept' => '*/*', ... }.
|
HEADER is a Hash like { 'Accept' => '*/*', ... }.
|
||||||
|
|
||||||
This method returns a Net::HTTPResponse object.
|
This method returns a Net::HTTPResponse object.
|
||||||
You can http header from this object like:
|
|
||||||
|
In version 1.1, this method might raises exception for also
|
||||||
|
3xx (redirect). On the case you can get a HTTPResponse object
|
||||||
|
by "anException.response".
|
||||||
|
|
||||||
response = nil
|
response = nil
|
||||||
Net::HTTP.start( 'some.www.server', 80 ) {|http|
|
Net::HTTP.start( 'some.www.server', 80 ) {|http|
|
||||||
@ -274,6 +281,10 @@ Yes, this is not thread-safe.
|
|||||||
|
|
||||||
If called with block, gives a part of entity body string.
|
If called with block, gives a part of entity body string.
|
||||||
|
|
||||||
|
In version 1.1, this method might raises exception for also
|
||||||
|
3xx (redirect). On the case you can get a HTTPResponse object
|
||||||
|
by "anException.response".
|
||||||
|
|
||||||
# version 1.1
|
# version 1.1
|
||||||
response, body = http.post( '/index.html', 'querytype=subject&target=ruby' )
|
response, body = http.post( '/index.html', 'querytype=subject&target=ruby' )
|
||||||
# version 1.2
|
# version 1.2
|
||||||
@ -292,13 +303,15 @@ Yes, this is not thread-safe.
|
|||||||
http.post '/index.html', 'querytype=subject&target=ruby', nil, f
|
http.post '/index.html', 'querytype=subject&target=ruby', nil, f
|
||||||
}
|
}
|
||||||
|
|
||||||
: request( request, [data] )
|
: request( request [, data] )
|
||||||
: request( request, [src] ) {|response| .... }
|
: request( request [, data] ) {|response| .... }
|
||||||
sends REQUEST to (remote) http server. This method also writes
|
sends a HTTPRequest object REQUEST to (remote) http server.
|
||||||
string from DATA string if REQUEST is a post/put request.
|
This method also writes string from DATA string if REQUEST is
|
||||||
(giving DATA for get/head request causes ArgumentError.)
|
a post/put request. Giving DATA for get/head request causes
|
||||||
|
ArgumentError.
|
||||||
|
|
||||||
If called with block, gives a HTTPResponse object to the block.
|
If called with block, gives a HTTPResponse object to the block
|
||||||
|
with connecting server.
|
||||||
|
|
||||||
== class Net::HTTP::Get, Head, Post
|
== class Net::HTTP::Get, Head, Post
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ This example retrieves mails from server and delete it (on server).
|
|||||||
Mails are written in file named 'inbox/1', 'inbox/2', ....
|
Mails are written in file named 'inbox/1', 'inbox/2', ....
|
||||||
Replace 'pop3.server.address' your POP3 server address.
|
Replace 'pop3.server.address' your POP3 server address.
|
||||||
|
|
||||||
|
|
||||||
require 'net/pop'
|
require 'net/pop'
|
||||||
|
|
||||||
Net::POP3.start( 'pop3.server.address', 110,
|
Net::POP3.start( 'pop3.server.address', 110,
|
||||||
@ -38,9 +37,9 @@ Replace 'pop3.server.address' your POP3 server address.
|
|||||||
else
|
else
|
||||||
i = 0
|
i = 0
|
||||||
pop.each_mail do |m| # or "pop.mails.each ..."
|
pop.each_mail do |m| # or "pop.mails.each ..."
|
||||||
File.open( 'inbox/' + i.to_s, 'w' ) do |f|
|
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
|
||||||
f.write m.pop
|
f.write m.pop
|
||||||
end
|
}
|
||||||
m.delete
|
m.delete
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
@ -51,18 +50,22 @@ Replace 'pop3.server.address' your POP3 server address.
|
|||||||
=== Shorter Version
|
=== Shorter Version
|
||||||
|
|
||||||
require 'net/pop'
|
require 'net/pop'
|
||||||
i = 0
|
|
||||||
Net::POP3.start( 'pop3.server.address', 110,
|
Net::POP3.start( 'pop3.server.address', 110,
|
||||||
'YourAccount', 'YourPassword' ) {|pop|
|
'YourAccount', 'YourPassword' ) {|pop|
|
||||||
|
if pop.mails.empty? then
|
||||||
|
puts 'no mail.'
|
||||||
|
else
|
||||||
|
i = 0
|
||||||
pop.delete_all do |m|
|
pop.delete_all do |m|
|
||||||
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
|
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
|
||||||
f.write m.pop
|
f.write m.pop
|
||||||
}
|
}
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
And this is more shorter example.
|
And here is more shorter example.
|
||||||
|
|
||||||
require 'net/pop'
|
require 'net/pop'
|
||||||
i = 0
|
i = 0
|
||||||
@ -129,20 +132,20 @@ net/pop also supports APOP authentication. There's two way to use APOP:
|
|||||||
end
|
end
|
||||||
|
|
||||||
: foreach( address = 'localhost', port = 110, account, password ) {|mail| .... }
|
: foreach( address = 'localhost', port = 110, account, password ) {|mail| .... }
|
||||||
starts protocol and iterate for each POPMail object.
|
starts POP3 protocol and iterates for each POPMail object.
|
||||||
This method equals to
|
This method equals to
|
||||||
|
|
||||||
Net::POP3.start( address, port, account, password ) do |pop|
|
Net::POP3.start( address, port, account, password ) {|pop|
|
||||||
pop.each do |m|
|
pop.each_mail do |m|
|
||||||
yield m
|
yield m
|
||||||
end
|
end
|
||||||
end
|
}
|
||||||
|
|
||||||
Typical usage:
|
# example
|
||||||
|
Net::POP3.foreach( 'your.pop.server', 110,
|
||||||
Net::POP3.foreach( addr, nil, acnt, pass ) do |m|
|
'YourAccount', 'YourPassword' ) do |m|
|
||||||
m.pop file
|
file.write m.pop
|
||||||
m.delete
|
m.delete if $DELETE
|
||||||
end
|
end
|
||||||
|
|
||||||
: delete_all( address = 'localhost', port = 110, account, password )
|
: delete_all( address = 'localhost', port = 110, account, password )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user