Gossamer Forum
Home : General : Perl Programming :

IO::Socket

Quote Reply
IO::Socket
Has anyone had any experience using IO::Socket??? I have a script that uses it to bind port 80 and parse info from remote sites. Unfortunately, it doesn't run correctly on my current setup. I am running it in Apache on Linux and I don't think it has the correct access to bind a port. I put the script on a free hosting site who is running FreeBSD, and it works fine. The only conclusion I can come up with is that on Linux (by default) Apache is running under nobody, nobody, and under FreeBSD (by default), it is running under www, www. The www account must provide the access needed for this to function. I have been planning to migrate my web server over to FreeBSD anyway (I already migrated my database server), so we'll see if that fixes it. Anyway, I just wondered if anyone had any experience with this.

Sean
Quote Reply
Re: [SeanP] IO::Socket In reply to
Yeah I've used it.

My apache runs as apache/apache. I use redhat 7.1

I use it to check if a pop3 server is running or not.
Quote Reply
Re: [Paul] IO::Socket In reply to
Did you create the apache/apache account/group? I am running RH 7.3 and compiled apache using mod_perl. I don't think it created any accounts or groups by default. Do you know what level of access is needed for binding a port?

Sean
Quote Reply
Re: [SeanP] IO::Socket In reply to
For binding to a port < 1024, you must be running as root. That's probably the problem. Otherwise it's likely that Apache is already bound to Port 80 (webserver port) and that's why it won't run.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] IO::Socket In reply to
I don't understand how it runs on my free hosting site. They're running FreeBSD. I'm sure they're not running apache using root. So, what steps do I need to take? Do I need to change the port apache uses from 80 to something else, and just NAT port 80 on my firewall to the new port? Also, should I setup a suid on that script to give it the correct access?

Sean

Last edited by:

SeanP: Jun 7, 2002, 9:59 AM
Quote Reply
Re: [SeanP] IO::Socket In reply to
Hi,

Is the script binding to port 80, or is it connecting to port 80? Big difference. =) If it's binding to port 80, then it must be run as root, and would not be able to run at the same time as the webserver. If it's just connecting to port 80, then it can run as any user.

What's the socket code look like that is failing?

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] IO::Socket In reply to
Here's part of it... I think it's failing during this piece:

Code:
########## definition of Socket.
use IO::Socket;

############# definition of Alarm(Timeout Function).

$sig{ALRM} = sub { exit (0); };

alarm ($TIMEOUT);

$modxxr =~ s/\n//s;

if (-e "$comftaf/$modxxr") {

require "$comftaf/$modxxr";

}

else {

exit (0);

}

if ($senam && $url && $parse) {
}

else {

exit (0);

}

#######

open (modka, "$comftaf/$modxxr");

@moduka = <modka>;

close (modka);

$moduka2 = "";

foreach $moduka3 (@moduka) {
$moduka2 .= $moduka3;
}

if (!$sprotocol) {
$sprotocol = "tcp";
}

if (!$speerport) {
$speerport = "80";
}

if (!$sreuse) {
$sreuse = "1";
}

if (!$shtmlsta) {
$shtmlsta = "<html.*?>";
}

if (!$shtmlend) {
$shtmlend = "</html>";
}

########

open (md1, ">>$cacdtaf/metayak-$countx-$ifor");

print md1 "$senam\n";

close (md1);

########################
##
##
##

$url1 = "$url";
$url1 =~ s/http:\/\///s;
@urlnfile1 = split (/\//, $url1);
$filex1 = $url1;
$filex1 =~ s/$urlnfile1[0]\///s;
$server1 = "$urlnfile1[0]";
$document1 = "$filex1";
###
$EOL = "\015\012";
$BLANK = $EOL x 2;
###
$remote1 = IO::Socket::INET->new(Proto=>"$sprotocol", PeerAddr=>$server1, PeerPort=>"$speerport", Reuse => $sreuse,);
$remote1->autoflush(1);
###
if ($rfullurl eq "yes") {
print $remote1 "GET $url HTTP/1.0" . $BLANK;
}
else {
print $remote1 "GET /$document1 HTTP/1.0" . $BLANK;
}
###
$sw2x = 0;
while ($input1 = <$remote1>) {

if ($input1 =~ /$shtmlsta/i) {
$sw1 = 1;
}
if ($input1 =~ /$shtmlend/i) {

$result .= $input1;

$sw1 = "";

close $remote1;

$sw2x = 1;

close (md1);

}
if ($sw1) {

$result .= $input1;

}
}

#############

It's the script you helped me with (the one I had Windows linefeeds in...). It runs now, but doesn't return any results.

Sean
Quote Reply
Re: [SeanP] IO::Socket In reply to
There are quite a few problems with that script, and the lack of error checking makes it hard to figure out what went wrong. Also, the URL fetching code isn't going to work on any name based servers as there is no Host: line. That could be what's wrong as well. Your going to need to do some debugging, and figure out exactly what is going on. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [SeanP] IO::Socket In reply to
Are you trying to fetch a webpage?
Quote Reply
Re: [Alex] IO::Socket In reply to
What's weird, is that it works just fine on the free web hosting site I used. I sent you a pm with the link...

Sean
Quote Reply
Re: [Paul] IO::Socket In reply to
In Reply To:
Are you trying to fetch a webpage?

Yes, it's parsing the html code from remote sites...

Sean
Quote Reply
Re: [SeanP] IO::Socket In reply to
How about using LWP::Simple ?
Quote Reply
Re: [Paul] IO::Socket In reply to
That would be my choice, but I didn't write this script. I'm just trying to get it to run on my servers. It doesn't look to be very clean code, but it works on the free hosting site. Hmm...

Sean