Basic Ruby http 服务器在 win7 上不向本地主机显示 .jpg?
Basic Ruby http server not displaying .jpg to localhost on win7?
这个 http 脚本在我的 Ubuntu 上的 gnome 终端上工作得很好(根据 Aleksey,在 Mac 上),但是在 win7 上,一个小方块被加载到chrome 浏览器。我需要做什么才能通过本地主机发送 JPEG,以便它显示在 win7 浏览器中?根据 Holger 的评论,我需要解决内容编码问题,但到目前为止我尝试的所有内容在 win7 上都没有任何区别(并且在 Ubuntu 中仍然可以正常加载而无需任何显式内容编码)。 .
PS C:\Users\user_name\Ruby\http_test> ls
basic_http.rb
lolcat.jpg
PS C:\Users\user_name\Ruby\http_test> ruby basic_http.rb
# very basic http server
require 'socket'
def send_200(socket, content)
socket.puts "HTTP/1.1 200 OK\r\n\r\n#{content}" # <-- Correct? (Per Holger)
socket.close
end
server = TCPServer.new 2016
loop do
Thread.start(server.accept) do |client|
request = client.gets
if request.start_with?("GET")
url = request.split(" ")[1]
if url.start_with?("/images/")
file = url.sub("/images/", "")
picture = File.read(file) # <-- initially Aleksey pointed out
send_200(client, picture) # <-- a variable name mismatch here
else # pictures/picture... heh.
send_200(client, "hello!")
end
end
end
end
FWIW:Ruby 2.2,win7 和编码 demo。
您只是在变量名中输入错误。
您将文件读取到 pictures
pictures = File.read(file)
但是你发送的是picture
send_200(client, picture)
所以你只需要编辑变量名。
也许将请求处理包装到 begin
块中是个好主意。
Thread.start(server.accept) do |client|
begin
...
rescue => ex
puts ex
end
end
这样你就可以看到是否有问题。
要在 win7 系统上将 jpeg 加载到浏览器中,File.read 命令需要显式解决二进制内容编码,例如File.binread("foo.bar")
每:
https://ruby-doc.org/core-2.1.0/IO.html#method-c-binread
if url.start_with?("/images/")
file = url.sub("/images/", "")
picture = File.binread(file) # <-- Thank you Holger & Aleksey!!!
send_200(client, picture)
感谢 Aleksey 和 Holger!
这个 http 脚本在我的 Ubuntu 上的 gnome 终端上工作得很好(根据 Aleksey,在 Mac 上),但是在 win7 上,一个小方块被加载到chrome 浏览器。我需要做什么才能通过本地主机发送 JPEG,以便它显示在 win7 浏览器中?根据 Holger 的评论,我需要解决内容编码问题,但到目前为止我尝试的所有内容在 win7 上都没有任何区别(并且在 Ubuntu 中仍然可以正常加载而无需任何显式内容编码)。 .
PS C:\Users\user_name\Ruby\http_test> ls
basic_http.rb
lolcat.jpg
PS C:\Users\user_name\Ruby\http_test> ruby basic_http.rb
# very basic http server
require 'socket'
def send_200(socket, content)
socket.puts "HTTP/1.1 200 OK\r\n\r\n#{content}" # <-- Correct? (Per Holger)
socket.close
end
server = TCPServer.new 2016
loop do
Thread.start(server.accept) do |client|
request = client.gets
if request.start_with?("GET")
url = request.split(" ")[1]
if url.start_with?("/images/")
file = url.sub("/images/", "")
picture = File.read(file) # <-- initially Aleksey pointed out
send_200(client, picture) # <-- a variable name mismatch here
else # pictures/picture... heh.
send_200(client, "hello!")
end
end
end
end
FWIW:Ruby 2.2,win7 和编码 demo。
您只是在变量名中输入错误。
您将文件读取到 pictures
pictures = File.read(file)
但是你发送的是picture
send_200(client, picture)
所以你只需要编辑变量名。
也许将请求处理包装到 begin
块中是个好主意。
Thread.start(server.accept) do |client|
begin
...
rescue => ex
puts ex
end
end
这样你就可以看到是否有问题。
要在 win7 系统上将 jpeg 加载到浏览器中,File.read 命令需要显式解决二进制内容编码,例如File.binread("foo.bar")
每:
https://ruby-doc.org/core-2.1.0/IO.html#method-c-binread
if url.start_with?("/images/")
file = url.sub("/images/", "")
picture = File.binread(file) # <-- Thank you Holger & Aleksey!!!
send_200(client, picture)
感谢 Aleksey 和 Holger!