0

I'm new to the requests part of Python, and having some trouble here.. I have the following example in Java, that works perfectly...

public class teste {
    //The variable “text” must have all parameters. For instance, it could be
    //-b –m 4 –d –tree removeAll –t TEXT, TEXT, TEXT
    private static void sendPost(String text) throws Exception {

        //Sobek’s URL
        URL url = new URL("http://sobek.ufrgs.br/webservice/sobek.php");
        //Please pay attention to the constant value “data”
        String postData = "data= "+ URLEncoder.encode(text);
        byte[] postDataBytes = postData.getBytes();
        //Connect to the URL
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        //Send the information
        conn.getOutputStream().write(postDataBytes);
        //Read the server response
        Reader in = new BufferedReader(new
        InputStreamReader(conn.getInputStream(), "UTF-8"));
        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);

    }
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String params = "-b –m 4 – tree removeAll -t TEXT";
        sendPost(params);
    }

}

but my software is in Python, so I need to transform the code.. I did this:

sobekURL = "sobek.ufrgs.br/webservice/sobek.php"

file_name = "Livro_MidiasNaEducacao_P1_rev3-cris2107.docx"

text = docx2txt.process(file_name)

parameters = "-b –m 4 – tree removeAll -t " + text

postData = "data=" + urllib.parse.quote_plus(parameters)
postDataBytes = bytearray(postData, 'utf8')

headers={
    "Content-type"  : "application/x-www-form-urlencoded",
    "Content-size"  : str(len(postDataBytes)) 
}
conn = http.client.HTTPConnection(sobekURL)
conn.request("POST",parameters,headers)
response=conn.getresponse()

print(response.status, response.reason)

data=response.read()

print(data)

conn.close()
conn.setRequestProperty();

I've got a lot of errors, solving it 1by1.. but now I got this:

Traceback (most recent call last): File "C:\Users\alvarosps\workspace\SobekMiningCorpus\AccessWebService.py", line 23, in conn.request("POST",parameters,headers) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1250, in _send_request self.putrequest(method, url, **skips) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1117, in putrequest self._output(request.encode('ascii')) UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 8: ordinal not in range(128)

Can anyone help?

EDIT: I've changed the argument parameters to parameters.encode("utf-8") and solved this problem.. but now I'm getting this: **

Traceback (most recent call last): File "C:\Users\alvarosps\workspace\SobekMiningCorpus\AccessWebService.py", line 23, in conn.request("POST", "/", parameters.encode('utf-8'),headers) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 964, in send self.connect() File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Users\alvarosps\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

**

It's not the same question as the link posted, it uses different libraries, and the answers there didn't solved anything here..

alvarosps
  • 83
  • 1
  • 9
  • Possible duplicate of ["getaddrinfo failed", what does that mean?](https://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean) – Joe Sep 27 '17 at 13:16
  • I saw that question..but it uses other library(socket), and the answers there didn't help me.. @Joe – alvarosps Sep 27 '17 at 13:23
  • You are simply using `http.client()` wrong here. You’d have to parse out the hostname first. Much easier to use `urllib.request` however. Or just install `requests`. – Martijn Pieters Jul 14 '19 at 19:42

0 Answers0