How come this doesn't work?

H

Hey You

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.
 
P

Phillip Gawlowski

Hey said:
/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.
 
B

Björn Paetzel

Hey said:
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.

Just a few suggestions:

1. Put POSTed form variables in the request body.

2. Specify the correct Content-Type for it
(application/x-www-form-urlencoded).

3. Use the "Connection: Keep-Alive" header to be able to do multiple
HTTP-Requests on the same connection. (And check whether the server
supports this)

4. Supply a correct Content-Length header, so the server knows where the
next request starts.

5. Have a look at Net::HTTP instead of doing it manually.

6. Don't post your username and password to a public newsgroup.

;)
 
M

Marcin Raczkowski

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)
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 11. Apr 2007, 20:10:22 +0900 schrieb Hey You:
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

1. indenting.
2. `else' section is for cleanup. Working code belongs just before
`rescue'.
3. Don't forget h.close or use h.open
3. 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
 
F

Francis Cianfrocca

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.

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

Hey You

Francis said:
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?
 
H

Hey You

Hey said:
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.
 
H

Hey You

Jason said:
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?
 
C

Chad Perrin

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.
 
H

Hey You

Chad said:
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.
 
B

Bill Kelly

From: "Hey You said:
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
 
E

Eleanor McHugh

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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top