Common way to read/write to/from TCPSocket

Y

yspro

Hi there! I want to know common way to create TCPSocket, write http requestand get response.

My simple function:

def read_from_sock(host, port)
sock = TCPSocket.new(host, port)

request = ''
request << "GET / HTTP/1.1\r\n"
request << "Host: #{host}\r\n"
request << "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1(KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n"
request << "\r\n"

sock.write(request)
response = ''

until sock.eof?
response << sock.read
end

sock.close
response
end



read_from_sock('yahoo.com', 80) returns

"HTTP/1.1 301 Redirect\r\nDate: Fri, 29 Mar 2013 06:56:20 GMT\r\nConnection: close\r\nServer: YTS/1.20.13\r\nCache-Control: no-store\r\nContent-Type: text/html\r\nContent-Language: en\r\nLocation: http://www.yahoo.com/\r\nContent-Length: 211\r\n\r\n<HEAD><TITLE>Redirect</TITLE></HEAD>\n<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">\n<FONT FACE=\"Helvetica,Arial\"><B>\n \"<em>http://www.yahoo.com/</em>\".<p></B></FONT>\n\n<!-- default \"Redirect\" response (301) -->\n</BODY>\n\u0000"

read_from_sock('facebook.com', 80) or read_from_sock('google.com', 80) hangs forever

Where is my mistake?
 
R

Robert Klemme

Hi there! I want to know common way to create TCPSocket, write http request and get response.

My simple function:

def read_from_sock(host, port)
sock = TCPSocket.new(host, port)

request = ''
request << "GET / HTTP/1.1\r\n"
request << "Host: #{host}\r\n"
request << "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n"
request << "\r\n"

sock.write(request)

Try to insert a sock.close_write here.
response = ''

until sock.eof?
response << sock.read
end

You do not need that loop because sock.read will read until EOF.
sock.close
response
end



read_from_sock('yahoo.com', 80) returns

"HTTP/1.1 301 Redirect\r\nDate: Fri, 29 Mar 2013 06:56:20 GMT\r\nConnection: close\r\nServer: YTS/1.20.13\r\nCache-Control: no-store\r\nContent-Type: text/html\r\nContent-Language: en\r\nLocation: http://www.yahoo.com/\r\nContent-Length: 211\r\n\r\n<HEAD><TITLE>Redirect</TITLE></HEAD>\n<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">\n<FONT FACE=\"Helvetica,Arial\"><B>\n \"<em>http://www.yahoo.com/</em>\".<p></B></FONT>\n\n<!-- default \"Redirect\" response (301) -->\n</BODY>\n\u0000"

read_from_sock('facebook.com', 80) or read_from_sock('google.com', 80) hangs forever

Where is my mistake?

Why don't you use net/http?

Cheers

robert
 
S

Simon Krahnke

* yspro <[email protected]> (2013-03-29) said:
Hi there! I want to know common way to create TCPSocket, write http request and get response.

My simple function:

def read_from_sock(host, port)
sock = TCPSocket.new(host, port)

request = ''
request << "GET / HTTP/1.1\r\n"
request << "Host: #{host}\r\n"
request << "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n"
request << "\r\n"

sock.write(request)
response = ''

until sock.eof?
response << sock.read
end

sock.close
response
end



read_from_sock('yahoo.com', 80) returns

"HTTP/1.1 301 Redirect\r\nDate: Fri, 29 Mar 2013 06:56:20 GMT\r\nConnection: close\r\nServer: YTS/1.20.13\r\nCache-Control: no-store\r\nContent-Type: text/html\r\nContent-Language: en\r\nLocation: http://www.yahoo.com/\r\nContent-Length: 211\r\n\r\n<HEAD><TITLE>Redirect</TITLE></HEAD>\n<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">\n<FONT FACE=\"Helvetica,Arial\"><B>\n \"<em>http://www.yahoo.com/</em>\".<p></B></FONT>\n\n<!-- default \"Redirect\" response (301) -->\n</BODY>\n\u0000"

read_from_sock('facebook.com', 80) or read_from_sock('google.com', 80) hangs forever

Your code works with me after i applied Robert's changes and I commented
out the User-Agent line.

They all (haven't tried facebook) redirect with 301 code, there's
nothing wrong with that. If you plan to do a full http-client than good
luck, its quite a lot of work.

mfg, simon .... l
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top