Guest User

Untitled

a guest
Nov 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. ################################################################################
  6. # Load hash from CSV at startup
  7. ################################################################################
  8. open DATA, "mapping.csv";
  9. my %hash;
  10. while( <DATA> ) {
  11. chomp $_;
  12. my ($field1,$field2) = split /,/, $_;
  13. if( $field1 ne '' ) {
  14. $hash{$field1} = $field2;
  15. }
  16. }
  17. close DATA;
  18. print "Readyn";
  19.  
  20. ################################################################################
  21. # Answer queries forever
  22. ################################################################################
  23. use IO::Socket::INET;
  24.  
  25. # auto-flush on socket
  26. $| = 1;
  27. my $port=5000;
  28.  
  29. # creating a listening socket
  30. my $socket = new IO::Socket::INET (
  31. LocalHost => '127.0.0.1',
  32. LocalPort => $port,
  33. Proto => 'tcp',
  34. Listen => 5,
  35. Reuse => 1
  36. );
  37. die "cannot create socket $!n" unless $socket;
  38.  
  39. while(1)
  40. {
  41. # waiting for a new client connection
  42. my $client_socket = $socket->accept();
  43.  
  44. my $data = "";
  45. $client_socket->recv($data, 1024);
  46.  
  47. my $key=$data;
  48. chomp $key;
  49. my $reply = "ERROR: Not found $key";
  50. if (defined $hash{$key}){
  51. $reply=$hash{$key};
  52. }
  53. print "DEBUG: Received $key: Replying $replyn";
  54.  
  55. $client_socket->send($reply);
  56. # notify client that response has been sent
  57. shutdown($client_socket, 1);
  58. }
  59.  
  60. chmod +x go.pl
  61.  
  62. ./go.pl
  63.  
  64. socat - TCP:127.0.0.1:5000 <<< "1350772177"
  65. 1347092335
  66.  
  67. START=$SECONDS; tail -1000 *csv | awk -F, '{print $1}' |
  68. while read a; do echo $a | socat - TCP:127.0.0.1:5000 ; echo; done; echo $START,$SECONDS
Add Comment
Please, Sign In to add comment