예제
참고한 사이트
한가지 언어를 알고있을때는
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#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 |