How come this doesn't work?


#1

require ‘socket’
host = ‘www.neopets.com
begin
h = TCPSocket.new(host,80)
rescue
puts “error: #($!)”
else
h.print “POST
/login.phtml?username=r3MaDi&password=mangaka02&destination=/petcentral.phtml
HTTP/1.1\r\nContent-Type: text/html;
charset=UTF-8\r\nHost:”+host+"\r\n\r\n"
h.print “GET /objects.phtml?type=inventory HTTP/1.1\r\n\r\n”
a = h.read
puts a
File.open(‘Log.txt’,‘w’) do |f|
f.print a
end
end
gets

It just pretty much does the first request which is the post but then
when I tell it to get the other page it doesn’t do anything. I made this
code a few seconds ago so I am searching for the answer and I am reading
RFC2616.


#2

Maybe Connection: Keep-Alive would help? Google it.

Aur S.


#3

Hey Y. wrote:

/login.phtml?username=r3MaDi&password=mangaka02&destination=/petcentral.phtml

If that is a live password, CHANGE IT NOW!


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/

Rule of Open-Source Programming #33:

Don’t waste time on writing test cases and test scripts - your users are
your best testers.


#4

On Wednesday 11 April 2007 11:29, Phillip G. wrote:

Hey Y. wrote:

/login.phtml?username=r3MaDi&password=mangaka02&destination=/petcentral.p
html

If that is a live password, CHANGE IT NOW!

first of all - ROTFL

second - keep-alive or one connection per request (few web browsers
still do
that - so can you - and you should ensure that you close connection -
passing
a block to #open is prefered way)


#5

Hi,

Am Mittwoch, 11. Apr 2007, 20:10:22 +0900 schrieb Hey Y.:

charset=UTF-8\r\nHost:"+host+"\r\n\r\n"
h.print “GET /objects.phtml?type=inventory HTTP/1.1\r\n\r\n”
a = h.read
puts a
File.open(‘Log.txt’,‘w’) do |f|
f.print a
end
end
gets

  1. indenting.
  2. else' section is for cleanup. Working code belongs just beforerescue’.
  3. Don’t forget h.close or use h.open
  4. Each request needs an own connection. This is NOT a Ruby
    subject.

require ‘socket’
class String ; def crlf ; gsub “\n”, “\r\n” ; end ; end
begin
host = ‘www.justanotherhttpserver.com
puts “-”*32
[ “POST …”, “GET …” ].each { |request|
TCPSocket.open host, 80 do |h|
h.write request.crlf
puts h.read
puts “-”*32
end
}
rescue
puts “error: #$!”
end

Bertram


#6

Francis C. wrote:

On 4/11/07, Hey Y. removed_email_address@domain.invalid wrote:

charset=UTF-8\r\nHost:"+host+"\r\n\r\n"
when I tell it to get the other page it doesn’t do anything. I made this
code a few seconds ago so I am searching for the answer and I am reading
RFC2616.


Posted via http://www.ruby-forum.com/.

Is there some reason why you can’t use one of the many different ways
that Ruby provides for accessing web sites?
But why not use sockets? I just want to learn how to use sockets because
aren’t http libraries made up of sockets?


#7

Hey Y. wrote:

Francis C. wrote:

On 4/11/07, Hey Y. removed_email_address@domain.invalid wrote:

charset=UTF-8\r\nHost:"+host+"\r\n\r\n"
when I tell it to get the other page it doesn’t do anything. I made this
code a few seconds ago so I am searching for the answer and I am reading
RFC2616.


Posted via http://www.ruby-forum.com/.

Is there some reason why you can’t use one of the many different ways
that Ruby provides for accessing web sites?
But why not use sockets? I just want to learn how to use sockets because
aren’t http libraries made up of sockets?
Or what different ways do you guys suggest. I want it to be fast, thats
why I was sticking with “naked” sockets. If you guys can tell me a way
to access web sites as fast as sockets then I will try to learn that
instead.


#8

On 4/11/07, Hey Y. removed_email_address@domain.invalid wrote:


Posted via http://www.ruby-forum.com/.
Dealing with the HTTP protocol is not a good way to learn how to use
Sockets. Work with your own little socket server and client if you
really
want to know how to do low-level networking.

And PLEASE go change your password NOW. and don’t go posting it again.

Jason


#9

Jason R. wrote:

On 4/11/07, Hey Y. removed_email_address@domain.invalid wrote:


Posted via http://www.ruby-forum.com/.
Dealing with the HTTP protocol is not a good way to learn how to use
Sockets. Work with your own little socket server and client if you
really
want to know how to do low-level networking.

And PLEASE go change your password NOW. and don’t go posting it again.

Jason
I will but do you recommend another to access the web that is as fast as
sockets?


#10

On 4/11/07, Hey Y. removed_email_address@domain.invalid wrote:

charset=UTF-8\r\nHost:"+host+"\r\n\r\n"
when I tell it to get the other page it doesn’t do anything. I made this
code a few seconds ago so I am searching for the answer and I am reading
RFC2616.


Posted via http://www.ruby-forum.com/.

Is there some reason why you can’t use one of the many different ways
that Ruby provides for accessing web sites?


#11

On Thu, Apr 12, 2007 at 06:34:42AM +0900, Hey Y. wrote:

Jason
I will but do you recommend another to access the web that is as fast as
sockets?

I don’t know how to put this any more clearly.

I, for one, don’t know enough about network programming with Ruby to be
able to answer your question effectively. That aside, however, a better
version of your previous email would have said this instead:

I did but do you recommend another to access the web that is as fast
as sockets?

Once a password is passed into public viewing, your first priority
should always be to change it. It’s not something to get around to
later – it’s something to do first. In fact, if you haven’t already
done so by this point, chances are good someone is already making
malicious use of your password (unless you’re absurdly lucky). Jason
wasn’t just being mean when he emphasized the importance of changing
your password “NOW”. He’s trying to help you avoid the consequences of
a very serious error.


#12

From: “Hey Y.” removed_email_address@domain.invalid

But why not use sockets? I just want to learn how to use sockets because
aren’t http libraries made up of sockets?

Or what different ways do you guys suggest. I want it to be fast, thats
why I was sticking with “naked” sockets. If you guys can tell me a way
to access web sites as fast as sockets then I will try to learn that
instead.

There seem to be several things all mixed together here:

  • You want to learn sockets (good)
  • You are trying to learn sockets and HTTP at the same time
    (difficult)
  • You are worried about performance (bad: premature optimization)

If you’re interested in learning sockets, that’s great. But to speak
HTTP,
you’re going to have to handle the same cases the existing HTTP
libraries
do. Yes, your code will be using sockets, just like the HTTP libraries
are
using sockets. Your code will likely exhibit similar performance
characteristics as the HTTP library code, too, provided there’s nothing
grossly wrong with either your code or the library code.

Being concerned about how fast your program will be while you’re still
in a learning stage is only going to unnecessarily complicate your
decisions. For more information about this:
http://www.c2.com/cgi/wiki?PrematureOptimization

If your primary interest is learning sockets, you might want to start
with
a simple line-based chat server… If you really want to learn sockets
and HTTP at the same time, that’s your business–just keep in mind that
HTTP will add its own complexity to the problem above and beyond just
learning sockets.

Regards,

Bill


#13

Chad P. wrote:

On Thu, Apr 12, 2007 at 06:34:42AM +0900, Hey Y. wrote:

Jason
I will but do you recommend another to access the web that is as fast as
sockets?

I don’t know how to put this any more clearly.

I, for one, don’t know enough about network programming with Ruby to be
able to answer your question effectively. That aside, however, a better
version of your previous email would have said this instead:

I did but do you recommend another to access the web that is as fast
as sockets?

Once a password is passed into public viewing, your first priority
should always be to change it. It’s not something to get around to
later – it’s something to do first. In fact, if you haven’t already
done so by this point, chances are good someone is already making
malicious use of your password (unless you’re absurdly lucky). Jason
wasn’t just being mean when he emphasized the importance of changing
your password “NOW”. He’s trying to help you avoid the consequences of
a very serious error.
The thing is I did. I actually didn’t notice that I posted my password
but once I saw the post I changed my password. I am sorry if it seemed
like I just ignored his post, I just wanted to know which way you guys
recommend.


#14

On 11 Apr 2007, at 22:34, Hey Y. wrote:

again.

Jason
I will but do you recommend another to access the web that is as
fast as
sockets?

I admire your desire to do this stuff at the lowest possible level -
it’s a great way to learn. However I take task with your belief that
direct socket programming will be faster than using an HTTP wrapper
library: the round-trip cost of any network communication is orders-
of-magnitude greater than the runtime overhead of using even loosely-
written interpreted code and is the primary limitation on the ‘speed’
of your code.

Ellie

Eleanor McHugh
Games With Brains

raise ArgumentError unless @reality.responds_to? :reason