Solved

perl script writing empty file, sometimes HELP PLEASE

Posted on 2000-05-04
19
219 Views
Last Modified: 2010-03-05
I have a very annoying and troubling problem.

I have a perl script which calls a another cgi script from ups.com The script on ups.com just sends back a raw html stream which the perl script then parses, and writes out the results to a temp file on my server.

What is happening is about %80 of the time, the temp file is blank. I've tested the script on UPS.com and it never timed out once or sent back blank results once. So i dont think the problem is with ups.

What i really dont understand is why it works sometimes, and not other times. I would think it would either work, or not work, not both.

I dont think its a permissions problem on my server (NT4, IIS4, Service pack 5, latest version of perl from activestate) since if the permissions were wrong, the script would either not run, or not be able to write a temp file. but the script is defintely running, and its defintely writing a temp file. Its just writing them blank sometimes.

This script was working before. It stopped working when i replaced my server with a faster machine. So i can only assume something, somewhere, on the new machine isnt set correctly. But i have no clue what that might be.

I have a bulletin board system on the same machine thats all perl based and it seems to be working fine.

I REALLY need some help on this one, sine its been a problem for the last 2 days with no progress at all.

if anyone wants to look at the perl script, please let me know and i'd be more then glad to send it. its about 10k
0
Comment
Question by:merk
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
can you pl send a copy of the script to maneshr@hotmail.com??

thanks
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
go it. currently looking at it.
0
 

Author Comment

by:merk
Comment Utility
This might turn out to be a problem with ups after all. Their site is down right now so i have no way of testing this script at all. Perhaps they made some changes to the format of their response which caused the script not to capture the results.
0
 
LVL 16

Accepted Solution

by:
maneshr earned 75 total points
Comment Utility
exactly!! i have been trying for the last 10 mins. i have stripped down version of your code (157 lines) that goes to their site and gets the info you want and does the processing.

here is the stripped version i am using.

here i have added $|++ and switched on the debug mode.

when i do that and run the script from the command prompt i get socket ok and bind ok.

Also i used a further stripped down version of this script (47 lines) to get yahoo's home page. That worked fine!!

so again i think your script is fine. All the same i will wait for the ups site to come up and try the ups.pl script again.
===ups.pl
#!/usr/local/bin/perl

$|++;
$debug=1;

# UPS Socket code
$ShipCountry="US";
$upsAction = '4';
$upsProduct = '1DM';
$OriginPostalCode = '94555';
$DestZipCode = '94423';
$PackageWeight = '4.2';

$AF_INET = 2;
$SOCK_STREAM = 2;
$sockaddr = 'S n a4 x8';
$port = 80;
$them = 'www.ups.com';
$function = 'GET';              #Type of HTTP
$workFile = '/using/services/rave/qcostcgi.cgi';
$versionInfo = "HTTP/1.0\n\n";

$workString = '?';
$workString .= 'accept_UPS_license_agreement=yes';
$workString .= '&';
$workString .= "10_action=$upsAction";
$workString .= '&';
$workString .= "13_product=$upsProduct";
$workString .= '&';
$workString .= "15_origPostal=$OriginPostalCode";
$workString .= '&';
$workString .= "22_destCountry=$ShipCountry";
$workString .= '&';
$workString .= "19_destPostal=$DestZipCode";
$workString .= '&';
$workString .= "23_weight=$PackageWeight";
$request = "$function $workFile$workString $versionInfo";

$SOCK_STREAM = 1;

($name, $aliases, $proto) = getprotobyname ('tcp');
($name, $aliases, $port) = getservbyname ($port, 'tcp') unless $port =~ /^\d+$/
;
($name, $aliases, $type, $len, $remoteaddr) = gethostbyname ($them);

$local = pack ($sockaddr, $AF_INET, 0, $localaddr);
$remote = pack ($sockaddr, $AF_INET, $port, $remoteaddr);

if (socket (S, $AF_INET, $SOCK_STREAM, $proto)) {
        print "socket ok\n" if $debug;
} else {
        die "socket: $!\n";
}

if (bind (S, $local)) {
        print "bind ok\n" if $debug;
} else {
        die "bind: $!\n";
}

if (connect (S, $remote)) {
        print "connect ok\n" if $debug;
} else {
        die "connect: $!\n";
}

select (S);
$| = 1;
select (STDOUT);
$| = 1;

print "$request\n" if $debug;
print S "$request\n";

$got_hdr = 0;
while (<S>) {
        print if $debug;
        push @result, $_ if $got_hdr;
        $got_hdr = 1 if /^\s*$/;        # get lines after header
}
close (S);
# done with socket code

# put the results in simple variables.
$j=0;
foreach $result (@result){
                # get the name/value strings

($code[$j],$ups[$j],$origin[$j],$ocountry[$j],$dest[$j],$odest[$j],$zone[$j],$w
eight[$j],$basechrg[$j],$surchrg[$j],$totalchrg[$j],$time[$j])
= split(/%/, $result);
                # list value keyed by numerical index
        if ($time[$j] eq "-1")  { $time[$j]="None"; }
        # add additional profit to ups charges
        if ($add_profit > 0) {$totalchrg[$j]=$totalchrg[$j]+$add_profit; }
        # Translate UPS Product Codes
        if ($ups[$j] eq "1DM")  { $ups[$j]="Next Day Air-AM"; }
        if ($ups[$j] eq "1DA")  { $ups[$j]="Next Day Air"; }
        if ($ups[$j] eq "1DP")  { $ups[$j]="Next Day Air Saver"; }
        if ($ups[$j] eq "2DM")  { $ups[$j]="2nd Day Air-AM"; }
        if ($ups[$j] eq "2DA")  { $ups[$j]="2nd Day Air"; }
        if ($ups[$j] eq "3DS")  { $ups[$j]="3 Day Select"; }
        if ($ups[$j] eq "GNDCOM")       { $ups[$j]="Ground Commercial"; }
        if ($ups[$j] eq "GNDRES")       { $ups[$j]="Ground Residential"; }

        # check for errors
                $j++;
        }

$upsmax = $j;

# build the code snippet that will be exchanged
for ($j=0; $j<$upsmax; $j++){
        $n=$j+1;
$htmlcode .= "UPS-$zone[$j]";
$htmlcode .= ",";
$htmlcode .= "$ups[$j]";
$htmlcode .= ",";
$htmlcode .= "$totalchrg[$j]";
$htmlcode .= "\n";
}

# Write out UPS file
                # get a temporary filename
do {
     $tempfile = $pathstring . int(rand(99999999)) . ".ups";
   } until !(-e $tempfile);

open (OUTPUT, ">$tempfile") || die "Can't open $tempfile for reading ($!) - Che
ck with your ISP";
select (OUTPUT);
print (OUTPUT $htmlcode) || die "Can't write UPS file: $! - Incorrect permissio
ns on cgi-bin
folder \n";
close (OUTPUT);

# redirect to shipping/db connector
# first add the tempfile name to the redirect string

print "content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>HELLO WORLD</title>\n";
print "</head>\n";
print "<body>\n";
print "<h2>UPS RESULTS</h2>\n";
print @result;
print "<h3>tester</h3>\n";
print "$htmlcode ";
print "</body>\n";
print "</html>\n";
0
 

Author Comment

by:merk
Comment Utility
well the reason i am not %100 sure its a probablem with UPS is because yesterday their site was up. And this script was writing empty temp files, instead of writing the results from the UPS site. When i would hit the UPS url myself, it always worked.

So the only thought i have now, is maybe UPS looked ok to me, but maybe something wasnt entirely ok to the script. Maybe the layout or formatting or something minor changed, which caused the script to ignore the results that UPS sends.

Thats the only thing i can think of right now to explain why it would write empty temp files.

When UPS comes back up i'll try it again.
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
"So the only thought i have now, is maybe UPS looked ok to me, but maybe something wasnt entirely ok to the script. Maybe the layout or formatting or something minor changed, which caused the script to ignore the results that UPS sends."


i think the most likely reason could be the formatting, since you are expecting the results to be % delimited.

anyway as of now (18:48 EST) its still now responding. :-(
hope it has nothing to do with the "I love you" virus!!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Are you sure $SOCK_STREAM = 1; is correct?
You may want to
use Socket;
to import the proper definitions
0
 

Author Comment

by:merk
Comment Utility
maneshr...if and when ups comes back up i will post the results of what i normally see when i put the url in the browser.

ozo...you just went over my head. no idea what you mean by using socket. I usually know enough about perl to maybe get a rought idea of what a small script is supposed to do. But thats about it.

thanks
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
merk,

sounds good!! meanwhile ups.com is still not respodning/slow.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
i ran the trimmed down version of your script a few minutes back and it worked fine!!

here are the contents of the .ups file that the script created.

UPS-94423,The Next Day Air Early A.M. service may not guarantee Early AM arriva
l to   94423 United States.,18.25
UPS-102,Next Day Air,18.25
UPS-202,2nd Day Air,8.20
UPS-302,3 Day Select,5.70
UPS-002,Ground Commercial,3.42
UPS-002,Ground Residential,4.42


meanwhile, how is the script behaving at your end??

did it work??

let me know
0
 

Author Comment

by:merk
Comment Utility
I havent had a chance to test your script out yet. but i had a thought from a problem someone else posted saying he was unable to make a socket call using perl.

I found a way around this problem. I was using the perlis.dll Instead i switched to using the perl.exe and mapped that to the script, and now its working fine.

Does that give you any clue? Why would it work with the exe but not the dll?

Also, the old server, which was using the dll, was perl 5.003. The new server is running perl 5.6
0
 

Author Comment

by:merk
Comment Utility
Maneshr, i found out the problem was perl itself. There is a glitch within perl 5.6 when making socket connections. I had to install perl 5.22 and now it seems to be working fine.

Considering all the help you gave me i dont want to just delete the question. If you would email me at merk@pacificnet.net, i will post another empty question with the points set to 75 and give you credit for that. I hope that seems fair.
0
 

Author Comment

by:merk
Comment Utility
This turned out to be a bug within perl itself. I had to install perl 5.22 to get it to work. I would like to give Maneshr though 75 points for the help he gave me.
0
 

Author Comment

by:merk
Comment Utility
This question has a deletion request Pending
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
This question no longer is pending deletion
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
Glad to know that your problem was solved.

i would suggest that you post a question with the title "For maneshr".

i will just answer that Q and you can go ahead & accept the answer.

Rgds
0
 
LVL 5

Expert Comment

by:ianB
Comment Utility
Community Support has reduced points from 150 to 75
0
 
LVL 5

Expert Comment

by:ianB
Comment Utility
Hi,

I am reducing the points on this question as asked so merk can award it to the expert for 75 points.

Ian
Community Support @ Experts Exchange
0
 

Author Comment

by:merk
Comment Utility
thanks for the help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now