""" remotetypograf.py python-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client) Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/ Typograf homepage: http://typograf.artlebedev.ru/ Web-service address: http://typograf.artlebedev.ru/webservices/typograf.asmx WSDL-description: http://typograf.artlebedev.ru/webservices/typograf.asmx?WSDL Default charset: UTF-8 Python version Author: Sergey Lavrinenko (s.lavrinenko@gmail.com) Version: 1.0 (2007-05-18) based on script by Andrew Shitov (ash@design.ru) Example: from RemoteTypograf import RemoteTypograf rt = RemoteTypograf() # rt = RemoteTypograf('windows-1251') print rt.processText ('"Вы все еще кое-как верстаете в "Ворде"? - Тогда мы идем к вам!"'); """ import socket class RemoteTypograf: _entityType = 4 _useBr = 1 _useP = 1 _maxNobr = 3 _encoding = 'UTF-8' def __init__(self, encoding='UTF-8'): self._encoding = encoding def htmlEntities(self): self._entityType = 1 def xmlEntities(self): self._entityType = 2 def mixedEntities(self): self._entityType = 4 def noEntities(self): self._entityType = 3 def br(self, value): self._useBr = 1 if value else 0 def p(self, value): self._useP = 1 if value else 0 def nobr(self, value): self._maxNobr = int(value) if value else 0 def processText(self, text): text = text.replace('&', '&') text = text.replace('<', '<') text = text.replace ('>', '>') SOAPBody = '\n' SOAPBody += '\n' SOAPBody += '\r\n' SOAPBody += ' \n' SOAPBody += ' %s\n' % text SOAPBody += ' %s\n' % self._entityType SOAPBody += ' %s\n' % self._useBr SOAPBody += ' %s\n' % self._useP SOAPBody += ' %s\n' % self._maxNobr SOAPBody += ' \n' SOAPBody += ' \n' SOAPBody += '\n' host = 'typograf.artlebedev.ru'; SOAPRequest = 'POST /webservices/typograf.asmx HTTP/1.1\n' SOAPRequest += 'Host: typograf.artlebedev.ru\n' SOAPRequest += 'Content-Type: text/xml\n' SOAPRequest += 'Content-Length: %d\n' % len(bytearray(SOAPBody, 'utf-8')) SOAPRequest += 'SOAPAction: "http://typograf.artlebedev.ru/webservices/ProcessText"\n\n' SOAPRequest += SOAPBody remoteTypograf = socket.socket(socket.AF_INET, socket.SOCK_STREAM) remoteTypograf.connect((host, 80)) remoteTypograf.sendall(bytearray(SOAPRequest, 'utf-8')) typografResponse = bytearray() while 1: buf = remoteTypograf.recv(8192) if len(buf) == 0: break typografResponse += buf remoteTypograf.close() typografResponse = typografResponse.decode(encoding='utf-8') startsAt = typografResponse.find('') + 19 endsAt = typografResponse.find('') typografResponse = typografResponse[startsAt:endsAt] typografResponse = typografResponse.replace('&', '&' ) typografResponse = typografResponse.replace('<', '<') typografResponse = typografResponse.replace ('>', '>') return typografResponse