<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: LOCAL code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:46:59 GMT</pubDate>
    <description>DZone Snippets: LOCAL code</description>
    <item>
      <title>Basic WEBrick setup for local web server</title>
      <link>http://snippets.dzone.com/posts/show/5659</link>
      <description>Put the following functions and aliases into your ~/.bash_login (or alternatives).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;unset -f webrick_local&lt;br /&gt;&lt;br /&gt;function webrick_local() { &lt;br /&gt;&lt;br /&gt;   declare root_dir="${HOME}/Desktop/www"&lt;br /&gt;&lt;br /&gt;   /bin/mkdir -p "${root_dir}/WEBrickLog"&lt;br /&gt;   /bin/chmod 0754 "${root_dir}" "${root_dir}/WEBrickLog"&lt;br /&gt;&lt;br /&gt;   /usr/bin/touch "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;   /bin/chmod 0644 "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;&lt;br /&gt;#   /usr/bin/touch "${root_dir}/index.html"&lt;br /&gt;#   /bin/chmod 0754 "${root_dir}/index.html"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/usr/local/bin/ruby &lt;&lt;-HEREDOC&lt;br /&gt;&lt;br /&gt;   require 'webrick'&lt;br /&gt;   require 'webrick/accesslog'&lt;br /&gt;   include WEBrick&lt;br /&gt;&lt;br /&gt;   require 'thread'&lt;br /&gt;   require 'pp'&lt;br /&gt;&lt;br /&gt;   root_dir = "${root_dir}"&lt;br /&gt;   http_dir = File.expand_path(root_dir)&lt;br /&gt;&lt;br /&gt;   File.open(http_dir + "/WEBrickLog/webrick_ruby.pid", "w") do |f|&lt;br /&gt;      f.puts(Process.pid)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # cf. http://microjet.ath.cx/webrickguide/html/Logging.html&lt;br /&gt;   webrick_log_file = File.expand_path(http_dir + "/WEBrickLog/webrick.log")&lt;br /&gt;   #webrick_log_file = '/dev/null'  # disable logging&lt;br /&gt;   webrick_logger = WEBrick::Log.new(webrick_log_file, WEBrick::Log::DEBUG)&lt;br /&gt;&lt;br /&gt;   access_log_stream = webrick_logger&lt;br /&gt;   access_log = [[ access_log_stream, WEBrick::AccessLog::COMBINED_LOG_FORMAT ]]&lt;br /&gt;&lt;br /&gt;   system_mime_table = WEBrick::HTTPUtils::load_mime_types('/private/etc/httpd/mime.types.default')&lt;br /&gt;   system_mime_table.store('rhtml', 'text/html')   # add a mime type for .rhtml files&lt;br /&gt;   system_mime_table.store('php', 'text/html')&lt;br /&gt;   system_mime_table.store('rb', 'text/plain')&lt;br /&gt;   system_mime_table.store('pid', 'text/plain')&lt;br /&gt;   #pp system_mime_table.sort_by { |k,v| k }&lt;br /&gt;&lt;br /&gt;   server = WEBrick::HTTPServer.new(&lt;br /&gt;     :BindAddress     =&gt;    "localhost",&lt;br /&gt;     :Port            =&gt;    9090,&lt;br /&gt;     :DocumentRoot    =&gt;    http_dir,&lt;br /&gt;     :FancyIndexing   =&gt;    true,&lt;br /&gt;     :MimeTypes       =&gt;    system_mime_table,&lt;br /&gt;     :Logger          =&gt;    webrick_logger,&lt;br /&gt;     :AccessLog       =&gt;    access_log&lt;br /&gt;   )&lt;br /&gt;&lt;br /&gt;   server.config.store(:DirectoryIndex, server.config[:DirectoryIndex] &lt;&lt; "default.htm")&lt;br /&gt;   #pp server.config&lt;br /&gt;&lt;br /&gt;   # cf. http://snippets.dzone.com/posts/show/5208&lt;br /&gt;   class TimeServlet &lt; HTTPServlet::AbstractServlet&lt;br /&gt;&lt;br /&gt;      def do_GET(req, res)&lt;br /&gt;         res['Content-Type'] = 'text/html'&lt;br /&gt;         res.status = 200&lt;br /&gt;         res.body = "&lt;html&gt;Time: #{Time.now.to_s}&lt;/html&gt;" + "\n"&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      # cf. http://www.hiveminds.co.uk/node/244, published under the&lt;br /&gt;      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html&lt;br /&gt;&lt;br /&gt;      @@instance = nil&lt;br /&gt;      @@instance_creation_mutex = Mutex.new&lt;br /&gt;&lt;br /&gt;      def self.get_instance(config, *options)&lt;br /&gt;         #pp @@instance&lt;br /&gt;         @@instance_creation_mutex.synchronize { &lt;br /&gt;            @@instance = @@instance || self.new(config, *options) }&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   # cf. http://ttripp.blogspot.com/2007/01/fun-with-http.html&lt;br /&gt;   class PostDumper &lt; WEBrick::HTTPServlet::AbstractServlet&lt;br /&gt;  &lt;br /&gt;      # Reload file for each request, instantly&lt;br /&gt;      # updating the server with code changes &lt;br /&gt;      # without needing a restart.&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      def PostDumper.get_instance( config, *options )&lt;br /&gt;         load __FILE__&lt;br /&gt;         PostDumper.new config, *options&lt;br /&gt;      end&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      # cf. http://www.hiveminds.co.uk/node/244, published under the&lt;br /&gt;      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html&lt;br /&gt;&lt;br /&gt;      @@instance = nil&lt;br /&gt;      @@instance_creation_mutex = Mutex.new&lt;br /&gt;&lt;br /&gt;      def self.get_instance(config, *options)&lt;br /&gt;         #pp @@instance&lt;br /&gt;         @@instance_creation_mutex.synchronize { &lt;br /&gt;            @@instance = @@instance || self.new(config, *options) }&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      def do_GET( request, response )&lt;br /&gt;         response.status = 200&lt;br /&gt;         response['Content-Type'] = "text/plain"&lt;br /&gt;         response.body = dump_request( request )&lt;br /&gt;      end&lt;br /&gt;  &lt;br /&gt;      def do_POST( request, response )&lt;br /&gt;         response.status = 200&lt;br /&gt;         response['Content-Type'] = "text/plain"&lt;br /&gt;         response.body = dump_request( request )&lt;br /&gt;         response.body &lt;&lt; request.body&lt;br /&gt;      end&lt;br /&gt;  &lt;br /&gt;      def dump_request( request )&lt;br /&gt;         request.request_line &lt;&lt; "\r\n" &lt;&lt;&lt;br /&gt;         request.raw_header.join( "" ) &lt;&lt; "\r\n"&lt;br /&gt;      end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   server.mount("/dump", PostDumper)&lt;br /&gt;   server.mount("/time", TimeServlet, {:FancyIndexing=&gt;true})&lt;br /&gt;&lt;br /&gt;   # handle signals&lt;br /&gt;   %w(INT).each do |signal|&lt;br /&gt;      trap(signal) { server.shutdown }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   server.start&lt;br /&gt;&lt;br /&gt;HEREDOC&lt;br /&gt;&lt;br /&gt;return 0&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;export -f webrick_local&lt;br /&gt;&lt;br /&gt;# start WEBrick alias&lt;br /&gt;alias webrick='swr'&lt;br /&gt;&lt;br /&gt;# start WEBrick&lt;br /&gt;unset -f swr&lt;br /&gt;function swr() {   &lt;br /&gt;  declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;  if [[ -e "${webrick_pid_file}" ]]; then echo "WEBrick already started!"; return 1; fi&lt;br /&gt;  /bin/bash -c "webrick_local" &amp;&lt;br /&gt;  declare WEBRICKPID=$!&lt;br /&gt;  mkdir -p "${HOME}/Desktop/www/WEBrickLog"&lt;br /&gt;  echo $WEBRICKPID &gt; "${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;  return 0&lt;br /&gt;}&lt;br /&gt;export -f swr&lt;br /&gt;&lt;br /&gt;# quit WEBrick&lt;br /&gt;unset -f qwr&lt;br /&gt;function qwr() { &lt;br /&gt;   declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;   declare webrick_ruby_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;   if [[ ! -e "${webrick_pid_file}" ]]; then echo "no such file: ${webrick_pid_file}"; return 1; fi&lt;br /&gt;   declare PIDW=$(cat "${webrick_pid_file}" 2&gt;/dev/null)&lt;br /&gt;   declare PIDR=$(cat "${webrick_ruby_pid_file}" 2&gt;/dev/null)&lt;br /&gt;   kill -INT $PIDW 2&gt;/dev/null || echo "no such PIDW: ${PIDW}"&lt;br /&gt;   kill -INT $PIDR 2&gt;/dev/null || echo "no such PIDR: ${PIDR}"&lt;br /&gt;   rm -f "${webrick_pid_file}" "${webrick_ruby_pid_file}"&lt;br /&gt;   return 0&lt;br /&gt;}&lt;br /&gt;export -f qwr&lt;br /&gt;&lt;br /&gt;# quit WEBrick; cf. Job Control Commands, http://tldp.org/LDP/abs/html/x8816.html&lt;br /&gt;###alias qwr='echo "quit WEBrick with ctrl-c ..."; fg %webrick 2&gt;/dev/null'&lt;br /&gt;&lt;br /&gt;alias openwww='/usr/bin/open http://localhost:9090'&lt;br /&gt;alias wrlog='/usr/bin/open http://localhost:9090/WEBrickLog/webrick.log'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#----------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;source ~/.bash_login&lt;br /&gt;&lt;br /&gt;webrick&lt;br /&gt;&lt;br /&gt;openwww&lt;br /&gt;wrlog&lt;br /&gt;open http://localhost:9090 &lt;br /&gt;open http://localhost:9090/WEBrickLog&lt;br /&gt;open http://localhost:9090/WEBrickLog/webrick.log&lt;br /&gt;&lt;br /&gt;open http://localhost:9090/time&lt;br /&gt;open http://localhost:9090/dump&lt;br /&gt;&lt;br /&gt;wrlog&lt;br /&gt;qwr&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Further information:&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://www.webrick.org"&gt;WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/webrick/rdoc/index.html"&gt;WEBrick RDoc&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.hiveminds.co.uk/node/245"&gt;What is WEBrick?&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.hiveminds.co.uk/node/244"&gt;Guide to using WEBrick for Rails development&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.igvita.com/2007/02/13/building-dynamic-webrick-servers-in-ruby/"&gt;Dynamic WEBrick Servers in Ruby&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://microjet.ath.cx/WebWiki/WEBrick.html"&gt;Gnome's Guide to WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://codeidol.com/other/rubyckbk/Internet-Services/Running-Servlets-with-WEBrick/"&gt;Running Servlets with WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://ruby.jamisbuck.org/rsphandler.rb"&gt;rsphandler&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://eigenclass.org/hiki/webrick-rewrite-rules"&gt;URL rewriting with WEBrick&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 17 Jun 2008 17:27:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5659</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Python - randomLocal</title>
      <link>http://snippets.dzone.com/posts/show/1942</link>
      <description>// Create a sample directory localIMGs&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import os&lt;br /&gt;import random&lt;br /&gt;&lt;br /&gt;class localImages(object):&lt;br /&gt;    &lt;br /&gt;    def __init__(self, directory='/home/Foto'):&lt;br /&gt;        &lt;br /&gt;        self.directoryScan = directory # Cartella da cui cominciare a prelevare le foto&lt;br /&gt;        self.directoryRemain = [self.directoryScan]&lt;br /&gt;        self.filePath = {}&lt;br /&gt;    &lt;br /&gt;    def getRandomImages(self):&lt;br /&gt;        '''&lt;br /&gt;        Prelieva dal proprio disco delle immagini in maniera random...&lt;br /&gt;        '''&lt;br /&gt;        &lt;br /&gt;        for i in range(5): # Scansiona 5 directory&lt;br /&gt;            if len(self.directoryRemain) &gt; 0:&lt;br /&gt;                directory = random.choice(self.directoryRemain)&lt;br /&gt;                self.directoryRemain.remove(directory)&lt;br /&gt;                &lt;br /&gt;                files = []&lt;br /&gt;                &lt;br /&gt;                try:&lt;br /&gt;                    files = os.listdir(directory)&lt;br /&gt;                except:&lt;br /&gt;                    pass&lt;br /&gt;                &lt;br /&gt;                # Serve per aumentare il Random delle Foto&lt;br /&gt;                # FIXME: cercare di aumentare questo random&lt;br /&gt;                if len(files) &gt; 0:&lt;br /&gt;                    for i in range(len(files)/2):&lt;br /&gt;                        files_remove = random.choice(files)&lt;br /&gt;                        files.remove(files_remove)&lt;br /&gt;                ##########################################&lt;br /&gt;                &lt;br /&gt;                for filename in files:&lt;br /&gt;                    filepath = os.path.join(directory, filename)&lt;br /&gt;                &lt;br /&gt;                    if os.path.isdir(filepath):&lt;br /&gt;                        self.directoryRemain.append(filepath)&lt;br /&gt;                    elif os.path.isfile(filepath):&lt;br /&gt;                        (name, ext) = os.path.splitext(filepath)&lt;br /&gt;                        if ext.lower() in ('.jpg', '.gif'):&lt;br /&gt;                            self.filePath[filepath] = 0&lt;br /&gt;                  &lt;br /&gt;        if len(self.directoryRemain) == 0: self.directoryRemain = [self.directoryScan]&lt;br /&gt;        &lt;br /&gt;    def downloadImages(self):&lt;br /&gt;        '''&lt;br /&gt;        Scarica nella cartella localIMGs le foto che vengono trovate nel disco...&lt;br /&gt;        '''&lt;br /&gt;        &lt;br /&gt;        numberIMGs = len(self.filePath)&lt;br /&gt;        posIMGs = 1&lt;br /&gt;        &lt;br /&gt;        for imageName in self.filePath:&lt;br /&gt;            print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName&lt;br /&gt;            fread = open(imageName, 'rb')&lt;br /&gt;            fwrite = open('localIMGs' + os.sep + os.path.split(imageName)[1], 'wb')&lt;br /&gt;            fwrite.write(fread.read())&lt;br /&gt;            fread.close()&lt;br /&gt;            fwrite.close()&lt;br /&gt;            posIMGs += 1&lt;br /&gt;                              &lt;br /&gt;if __name__ == '__main__':&lt;br /&gt;    &lt;br /&gt;    test = localImages()&lt;br /&gt;    &lt;br /&gt;    test.getRandomImages()&lt;br /&gt;    test.downloadImages()&lt;br /&gt;    &lt;br /&gt;    print 'Finito...'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Apr 2006 03:01:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1942</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Start MySQL with LOAD LOCAL INFILE enabled</title>
      <link>http://snippets.dzone.com/posts/show/1439</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/opt/mysql/bin/mysql -u root -h mysql-server --local-infile=1 &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 16:07:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1439</guid>
      <author>offspinner ()</author>
    </item>
    <item>
      <title>MySQL LOAD DATA LOCAL</title>
      <link>http://snippets.dzone.com/posts/show/1438</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;LOAD DATA LOCAL INFILE "/dc/elp/lionref/data/master/loggy.txt" INTO TABLE citations; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 16:06:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1438</guid>
      <author>offspinner ()</author>
    </item>
  </channel>
</rss>
