Category Archives: Script

[Perl] 빠른 입문을 위한 예제

한가지 언어를 알고있을때는

다른언어를 처음부터 배우기보다는
예제를 통해 익히는게
가장 습득속도가 빠른거같다.
예를들어 정독하는거보다 줄거리를 보는것 정도 ?!
방금 읽던책에 예제가 몇가지 나와서
열심히 타이핑해서 올려둔다
분량이 좀 있어서 파일로 =3=3=3

Ruby API 정리

 

#http://www.ruby-doc.org/core/ 
#base64_encode
 
  require 'base64'
  Base64.b64encode("Now is the time for all good coders\nto learn Ruby")
 
 
#http://www.ruby-doc.org/core/
#base64 decode
 require 'base64'
 str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
       'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
       'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
puts Base64.decode64(str)


Block TCP Socket

 
#http://rubylearning.com/satishtalim/ruby_socket_programming.html
#Block TCP Server

require "socket"  
dts = TCPServer.new('localhost', 20000)  
loop do  
 Thread.start(dts.accept) do |s|  
   print(s, " is accepted\n")  
   s.write(Time.now)  
   print(s, " is gone\n")  
   s.close  
 end  
end  
 
 
# p069dtclient.rb  
# http://rubylearning.com/satishtalim/ruby_socket_programming.html
# block TCP Client
 
require 'socket'  
streamSock = TCPSocket.new( "127.0.0.1", 20000 )  
#streamSock.send( "Hello\n" )  
str = streamSock.recv( 100 )  
print str  
streamSock.close



NonBlock TCP Socket

#www.ruby-doc.org/core
#nonblock TCP Server,Client
require "socket"
serv = TCPServer.new("127.0.0.1", 200020000)
af, port, host, addr = serv.addr
c = TCPSocket.new(addr,port)
s = serv.accept
c.send "12345678910", 0
IO.select([s])
p s.recv_nonblock(10) #=> "aaa"
print p


URI 인코딩/디코딩
require 'uri'
 enc_uri = URI.escape("http://example.com/?a=\11\15")
 p enc_uri
 # => "http://example.com/?a=%09%0D"
 p URI.unescape(enc_uri)
 # => "http://example.com/?a=\t\r"
 p URI.escape("@?@!", "!?")
 # => "@%3F@%21"


require 'uri'
 enc_uri = URI.escape("http://example.com/?a=\11\15")
 p enc_uri
 # => "http://example.com/?a=%09%0D"
 p URI.unescape(enc_uri)
 # => http://example.com/?a=\t\r


HTTP Get
        require 'net/http'
        require 'uri'
    Net::HTTP.get_print URI.parse('http://www.example.com/index.html')


HTTP Post
   require 'net/http'
   require 'uri'
   #1: Simple POST
   res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
                             {'q'=>'ruby', 'max'=>'50'})
   puts res.body

   #2: POST with basic authentication
   res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
   puts res.body
 
   #3: Detailed control
   url = URI.parse('http://www.example.com/todo.cgi')
   req = Net::HTTP::Post.new(url.path)
   req.basic_auth 'jack', 'pass'
   req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
   res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
   case res
   when Net::HTTPSuccess, Net::HTTPRedirection
     # OK
   else
     res.error!
   end

http://shampu.clus.org/tc/Shampu/entry/ruby-api-정리