Share Pastebin
Guest
Public paste!

Owen Stephens

By: a guest | Mar 20th, 2010 | Syntax: Python | Size: 0.88 KB | Hits: 67 | Expires: Never
Copy text to clipboard
  1. from __future__ import print_function
  2. import urllib
  3. import httplib
  4. import sys
  5. import re
  6.  
  7. if len(sys.argv) != 2:
  8.     print("Please enter latex source file name to POST.", file=sys.stderr)
  9.     exit()
  10.  
  11. input_file_name = sys.argv[1]
  12. input_file = open(input_file_name)
  13. input = input_file.read()
  14.  
  15. params = urllib.urlencode({'latexcode': input})
  16. headers = {"Content-type": "application/x-www-form-urlencoded"}
  17. conn = httplib.HTTPConnection("dna.uio.no:80")
  18. conn.request("POST", "/cgi-bin/texcount/texcount_2_2.cgi", params, headers)
  19. response = conn.getresponse()
  20.  
  21. if response:
  22.     data = response.read()
  23.     match = re.search("<dt>Words\s+in\s+text:\s+</dt>\s*<dd>(\d+)", data)
  24.    
  25.     if match is not None:
  26.         print("Count: " + match.group(1))
  27.     else:
  28.         print("Error parsing response.",file=sys.stderr)
  29.    
  30. else:
  31.     print("Error POSTing.", file=sys.stderr)