Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542326
  • 博文数量: 102
  • 博客积分: 950
  • 博客等级: 准尉
  • 技术积分: 1094
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-28 16:07
文章分类

全部博文(102)

文章存档

2020年(2)

2016年(5)

2015年(15)

2014年(25)

2013年(18)

2012年(19)

2011年(18)

我的朋友

分类: Web开发

2015-09-11 09:57:40

客户端:send_socket.pl
注意的是
    1. 回车符\r\n 的运用。还有boundary的分隔符。
     2. post参数可以加多个,文件也一样可以加多个。
    3. filename 是存在本地的文件名,可以不要自己定义名字。
   4. 如果用CGI.pm接收,name="Submit"  和 name="file" 必须写死,是需要的,否则服务器会一直等待超时!

命令:perl send_socket.pl 123.99.86.60 80

点击(此处)折叠或打开

  1. #!/usr/bin/perl -w
  2. # tcp_socket_cli.pl
  3. use strict;
  4. use Socket;

  5. my $addr = $ARGV[0] || '127.0.0.1';
  6. my $port = $ARGV[1] || '80';
  7. my $dest = sockaddr_in($port, inet_aton($addr));
  8. my $buf = undef;

  9. socket(SOCK,PF_INET,SOCK_STREAM,6) or die "Can't create socket: $!";
  10. connect(SOCK,$dest) or die "Can't connect: $!";

  11. my $pData = qq|------WebKitFormBoundary7MrX39PdxTZCRcIu\r\n| .
  12. qq|Content-Disposition: form-data; name="file"; filename="ss.txt"\r\n| .
  13. qq|Content-Type: text/plain\r\n\r\n| .
  14. qq|2\r\n| .
  15. qq|------WebKitFormBoundary7MrX39PdxTZCRcIu\r\n| .
  16. qq|Content-Disposition: form-data; name="filenewname"\r\n\r\n| .
  17. qq|x1111\r\n| .
  18. qq|------WebKitFormBoundary7MrX39PdxTZCRcIu\r\n| .
  19. qq|Content-Disposition: form-data; name="Submit"\r\n\r\n| .

  20. qq|Submit Form\r\n| .
  21. qq|------WebKitFormBoundary7MrX39PdxTZCRcIu--\r\n|
  22. ;

  23. my $len = length($pData);

  24. my $write_buf = qq|POST /clientupload/upload.cgi HTTP/1.1
  25. Host: logreport.xx.com.cn
  26. Connection: keep-alive
  27. Content-Length: 385
  28. Cache-Control: max-age=0
  29. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  30. Origin: http://logreport.xx.com.cn
  31. User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
  32. Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MrX39PdxTZCRcIu
  33. Referer: http://logreport.xx.com.cn/clientupload/uploadFile.pl
  34. Accept-Encoding: gzip,deflate,sdch
  35. Accept-Language: zh-CN,zh;q=0.8
  36. Cookie: _ga=GA1.3.2023535787.1437720833; __mtxud=26b02f269a3ac0c5.1437645511795.1438754742974.1438998340908.4; Hm_lvt_9e451c4b637507c34863250b1987332a=1440655977,1440656112,1441524498,1441766300; Hm_lpvt_9e451c4b637507c34863250b1987332a=1441766321\r\n\r\n
  37. $pData
  38. |;

  39. print "$write_buf";
  40. syswrite(SOCK, $write_buf);


  41. my $bs = sysread(SOCK, $buf, 2048); # try to read 2048
  42. print "Received $bs bytes, content $buf\n"; # actually get $bs bytes
  43. close SOCK;
服务器端,接收端:upload.cgi

点击(此处)折叠或打开

  1. #!/usr/bin/perl

  2. use strict;
  3. use CGI;
  4. use Fcntl qw( :DEFAULT :flock );

  5. use constant UPLOAD_DIR => "/data/clientLog/";
  6. use constant BUFFER_SIZE => 16_384;
  7. use constant MAX_FILE_SIZE => 1_048_576; # Limit each upload to 1 MB

  8. use constant MAX_DIR_SIZE => 100 * 1_048_576; # Limit total uploads to 100 MB

  9. use constant MAX_OPEN_TRIES => 100;

  10. $CGI::DISABLE_UPLOADS = 0;
  11. $CGI::POST_MAX = MAX_FILE_SIZE;

  12. my $q = new CGI;
  13. $q->cgi_error and error( $q, "Error transferring file: " . $q->cgi_error );
  14. print $q->header( -type=>'text/html', -charset=>'UTF=8');


  15. my $file = $q->param( "file" ) || error( $q, "No file received." );
  16. my $filename = $q->param( "filenewname" ) || error( $q, "No filename entered." );
  17. #my $fh = $q->upload( $file ) || die ( "No file uploaded. $file. $!" ); #程序在此处退出。
  18. my $fh = $q->upload( "file" ) || die ( "No file uploaded. $file. $!" ); #程序在此处退出。

  19. my $postData = $q->param( "postData" );
  20. my $TimeUpdate = $q->param( "TimeUpdate" );
  21. my $Account = $q->param( "Account" );
  22. my $ServerName = $q->param( "ServerName" );

  23. $q->print( "postData: $postData<br>" );
  24. $q->print( "postData: $TimeUpdate<br>" );
  25. $q->print( "postData: $Account<br>" );
  26. $q->print( "postData: $ServerName<br>" );

  27. my $io_handle;
  28. if( defined $fh ) {
  29.         $io_handle = $fh->handle;
  30. }

  31. #$q->print( "file: $file<br>filename: $filename<br>" );
  32. #$q->print( "UPLOAD_DIR: " . UPLOAD_DIR . " <br> BUFFER_SIZE: " .BUFFER_SIZE . "<br>" );
  33. #$q->print( "MAX_FILE_SIZE: " . MAX_FILE_SIZE . "<br>MAX_DIR_SIZE: " . MAX_DIR_SIZE . "<br>" );

  34. #my $type = $q->uploadInfo($filename)->{'Content-Type'};
  35. #$q->print( "type: $type<br>" );

  36. my $buffer = "";

  37. if ( dir_size( UPLOAD_DIR ) + $ENV{CONTENT_LENGTH} > MAX_DIR_SIZE ) {
  38.    error( $q, "Upload directory is full." );
  39. }

  40. # Allow letters, digits, periods, underscores, dashes

  41. # Convert anything else to an underscore

  42. $filename =~ s/[^\w.-]/_/g;
  43. if ( $filename =~ /^(\w[\w.-]*)/ ) {
  44.    $filename = $1;
  45. }
  46. else {
  47.    error( $q, "Invalid file name; files must start with a letter or number." );
  48. }

  49. # Open output file, making sure the name is unique
  50. #until ( sysopen OUTPUT, UPLOAD_DIR . $filename, O_CREAT | O_EXCL ) {
  51. unless ( sysopen OUTPUT, UPLOAD_DIR . $filename, O_RDWR | O_CREAT | O_EXCL ) {
  52.    $filename =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e;

  53.    $1 >= MAX_OPEN_TRIES and error( $q, "Unable to save your file." );
  54. }

  55. # This is necessary for non-Unix systems; does nothing on Unix
  56. binmode $fh;
  57. binmode OUTPUT;

  58. # Write contents to output file
  59. #while ( read( $fh, $buffer, BUFFER_SIZE ) ) {
  60. # print OUTPUT $buffer;
  61. #
  62. #}
  63. #
  64. while ( my $bytesread = $io_handle->read( $buffer, BUFFER_SIZE ) ) {
  65.    print OUTPUT $buffer;
  66. }

  67. close OUTPUT;
  68. $io_handle->close;

  69. print $q->start_html( "success upload" ),
  70.         $q->h1("Success"),
  71.         $q->p( "upload $filename");

  72. print $q->end_html;

  73. exit ;

  74. # -------- FUNCTION ----------
  75. sub dir_size {
  76.    my $dir = shift;

  77.    my $dir_size = 0;

  78.    # Loop through files and sum the sizes; doesn



阅读(1140) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~