Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # sat3310 - lab05
  4. # created by USERID@mtu.edu
  5. # DATE
  6.  
  7. use LWP::Simple;
  8.  
  9.  
  10. # Variables
  11. $dataurl = "http://www.tech.mtu.edu/~toarney/sat3310/lab07/";
  12. $datafile = "fortune500.tsv";
  13. $datapath = "/home/benakapoi/Documents/labs/lab05/data";
  14.  
  15. # Main
  16. getstore($dataurl.$datafile, $datapath.$datafile);
  17.  
  18. # Get a file - list of websites
  19.  
  20. # Parse websites file
  21. open(FILE, "<", $datapath.$datafile);
  22.  
  23. while ($line = <FILE>) {
  24. my @website=split("\t",$line);
  25. push @arrayofurls, $website[2];
  26. }
  27.  
  28. close FILE;
  29.  
  30. # Suffering from bufferring
  31. $|=1;
  32.  
  33. # Remove first element of array
  34. shift @arrayofurls;
  35.  
  36. # Print size of array
  37. $totalsize = scalar @arrayofurls;
  38. print "Total number of websites: $totalsize\n";
  39.  
  40. # Start the loop
  41. foreach my $website (@arrayofurls) {
  42. my $completeurl = "http://".$website;
  43. my ($type, $length, $modtime, $expiretime, $servertype) = head($completeurl);
  44. if ($servertype eq "") {$servertype = "Unknown"};
  45. if (index($servertype, "/") > 0) {
  46. $servertypesubstring = substr($servertype, 0, index($servertype, "/"));
  47. }
  48. else {
  49. $servertypesubstring = $servertype;
  50. }
  51. push (@arrayofservertypes, $servertypesubstring);
  52. $working++;
  53. print "Working... $working of $totalsize done.";
  54. print "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
  55. }
  56. print "\n";
  57.  
  58. # Sort results
  59.  
  60. # Count results
  61. %servertypehash = ();
  62. foreach my $servername (@arrayofservertypes) {
  63. $servertypehash{$servername}++;
  64. # print "Hash: $servername\t $servertypehash{$servername} \n";
  65. }
  66.  
  67. # Print results
  68. print "\nResults:\n";
  69. foreach my $server (sort { $servertypehash{$a} <=> $servertypehash{$b} } keys %servertypehash) {
  70. print "$servertypehash{$server} \t $server\n";
  71. }
  72.  
  73. # vi ~/Documents/labs/lab05/lab05.py
  74.  
  75. #!/usr/bin/python
  76.  
  77. # sat3310 - lab05
  78. # created by bcstinch@mtu.edu
  79. # 3/25/2018
  80.  
  81. # Modules
  82.  
  83. import urllib2
  84. import csv
  85. import collections
  86. import sys
  87.  
  88. # Variables
  89.  
  90. dataurl = 'http://www.tech.mtu.edu/~toarney/sat3310/lab07/'
  91. datafile = 'fortune500.tsv'
  92. datapath = '/home/benakapoi/Documents/labs/lab05/data/'
  93. myserverresponses = []
  94. websitecount = 0
  95.  
  96.  
  97. # Main
  98.  
  99. # Get a file - list of websites
  100.  
  101. downloadfile = urllib2.urlopen(dataurl + datafile)
  102. with open(datapath + datafile, 'w') as output:
  103. output.write(downloadfile.read())
  104. output.close()
  105.  
  106.  
  107. # Parse websites file
  108.  
  109. mywebsitecount = open(datapath + datafile).readlines()
  110. print "There are", len(mywebsitecount)-1, "websites in", datapath + datafile
  111.  
  112. with open(datapath + datafile, 'rt') as inputfile:
  113. next(inputfile)
  114. mywebsites = csv.reader(inputfile, delimiter='\t')
  115.  
  116. for rank,company,websites in mywebsites:
  117. request = urllib2.Request('http://' + websites)
  118. try:
  119. response = urllib2.urlopen(request,timeout=5)
  120. if response.info().getheader('Server') == '':
  121. myserverresponses.append ("Blank")
  122. else :
  123. my_server_type = str(response.info().getheader('Server'))
  124. my_server_type = my_server_type.split('/',1)
  125. myserverresponses.append (my_server_type[0])
  126. websitecount +=1
  127. except IOError, e:
  128. myserverresponses.append ("Error")
  129. websitecount +=1
  130. print "Processing:", websitecount, "of", len(mywebsitecount)-1,
  131. print "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
  132.  
  133.  
  134. # Sort, count, print result
  135.  
  136. print "\nResults\n"
  137. counter = collections.Counter(myserverresponses)
  138. for servers, freq in counter.most_common():
  139. print freq, "\t", servers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement