httr将工作中的Python连接翻译为R。

huangapple go评论100阅读模式
英文:

httr translating working python connection to r

问题

Here's the translation of the code parts you provided:

Python code:

  1. import requests
  2. import random
  3. import os
  4. import pandas as pd
  5. from rdkit import Chem
  6. upload_url = 'site name'
  7. def predict_pka(smi):
  8. param = {"Smiles": ("tmg", smi)}
  9. headers = {'token': 'tokenstring'}
  10. response = requests.post(url=upload_url, files=param, headers=headers)
  11. jsonbool = int(response.headers['ifjson'])
  12. if jsonbool == 1:
  13. res_json = response.json()
  14. if res_json['status'] == 200:
  15. pka_datas = res_json['gen_datas']
  16. return pka_datas
  17. else:
  18. raise RuntimeError("Error for prediction")
  19. else:
  20. raise RuntimeError("Error for prediction")
  21. if __name__ == "__main__":
  22. smi = "CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl"
  23. data_pka = predict_pka(smi)
  24. print(data_pka)

R code:

  1. getPKA <- function(){
  2. upload_url <- "site name"
  3. param <- rjson::toJSON(list('Smiles' = c("tmg", "CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl")))
  4. response <- httr::POST(url = upload_url,
  5. httr::add_headers(
  6. 'token' = 'tokenstring'
  7. ),
  8. encode = c("multipart", "form", "json", "raw"),
  9. httr::content_type_json(),
  10. body = param,
  11. httr::verbose()
  12. )
  13. return(response)
  14. }

Please note that the square brackets in R are used to define lists, which is equivalent to Python's normal brackets for defining dictionaries. The format of param in both versions should work correctly for sending a JSON request.

The error you're encountering might be related to the server's response, which is an internal server error (HTTP 500). You might need to check with the server administrators or consult their API documentation for further troubleshooting.

英文:

For a project i am trying to access data produced from an online model/algorithm. The owners provide the python code to access this data. This is as follows:

  1. import requests
  2. import random
  3. import os
  4. import pandas as pd
  5. from rdkit import Chem
  6. upload_url=r&#39;site name&#39;
  7. def predict_pka(smi):
  8. param={&quot;Smiles&quot; : (&quot;tmg&quot;, smi)}
  9. headers={&#39;token&#39;:&#39;tokenstring&#39;}
  10. response=requests.post(url=upload_url, files=param, headers=headers)
  11. jsonbool=int(response.headers[&#39;ifjson&#39;])
  12. if jsonbool==1:
  13. res_json=response.json()
  14. if res_json[&#39;status&#39;] == 200:
  15. pka_datas = res_json[&#39;gen_datas&#39;]
  16. return pka_datas
  17. else:
  18. raise RuntimeError(&quot;Error for prediction&quot;)
  19. else:
  20. raise RuntimeError(&quot;Error for prediction&quot;)
  21. if __name__==&quot;__main__&quot;:
  22. smi = &quot;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&quot;
  23. data_pka = predict_pka(smi)
  24. print(data_pka)

I took out the actual url and the token, since i don't know if its responsible to share those. This code works from R studio and using python, i can get the data.

However i want to get the data using an R script, so i tried translating the code to R:

  1. getPKA = function(){
  2. upload_url=&quot;site name&quot;
  3. param = rjson::toJSON(list(&#39;Smiles&#39; = c(&quot;tmg&quot;, &quot;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&quot;)))
  4. response = httr::POST(url = upload_url,
  5. httr::add_headers(
  6. &#39;token&#39; = &#39;tokenstring&#39;
  7. ),
  8. encode = c(&quot;multipart&quot;, &quot;form&quot;, &quot;json&quot;, &quot;raw&quot;),
  9. httr::content_type_json(),
  10. body = param,
  11. httr::verbose()
  12. )
  13. return(response)
  14. }

When i run the R code, i get the following output:

  1. -&gt; POST /modules/upload0/ HTTP/1.1
  2. -&gt; Host: host
  3. -&gt; User-Agent: libcurl/7.84.0 r-curl/5.0.0 httr/1.4.5
  4. -&gt; Accept-Encoding: deflate, gzip
  5. -&gt; Accept: application/json, text/xml, application/xml, */*
  6. -&gt; token: tokenstring
  7. -&gt; Content-Type: application/json
  8. -&gt; Content-Length: 56
  9. -&gt;
  10. &gt;&gt; {&quot;Smiles&quot;:[&quot;tmg&quot;,&quot;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&quot;]}
  11. &lt;- HTTP/1.0 500 INTERNAL SERVER ERROR
  12. &lt;- Content-Type: text/html; charset=utf-8
  13. &lt;- X-XSS-Protection: 0
  14. &lt;- Connection: close
  15. &lt;- Server: Werkzeug/1.0.1 Python/3.6.12
  16. &lt;- Date: Sat, 13 May 2023 10:28:53 GMT
  17. &lt;-

Once again i redacted the token and the host.

I got to this R code by reading up a bit on both the python requests package and the httr package, however i don't know much about API connections or web connections in general and i only need it for this data.

I think it might have to do with the param format.
When i print param in the python code i get this:
{&#39;Smiles&#39;: (&#39;tmg&#39;, &#39;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&#39;)}
while if i print it in the R code i get this: {&quot;Smiles&quot;:[&quot;tmg&quot;,&quot;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&quot;]}.
Normal brackets are used in the python version and square brackets are used in the R version.

I don't know if this is actually the problem or how to change this. I tried using different list types (vector, list) and i tried directly using the line {&#39;Smiles&#39;: (&#39;tmg&#39;, &#39;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&#39;)} from the python code as a character string in the body, but i get the same error.

If i print the response itself (or the content using content(response)), it says: AttributeError: &#39;NoneType&#39; object has no attribute &#39;filename&#39; among the html file contents.

I do see a lot of questions on stackoverflow with similar questions, and i tried copying their code and molding it for my needs, but it does not really change anything.

thank you for your time!

答案1

得分: 1

这个请求使用 requests 发送一个多部分编码的文件,请求的示例如下:

  1. POST / HTTP/1.1
  2. Host: localhost:1234
  3. User-Agent: python-requests/2.28.2
  4. Accept-Encoding: gzip, deflate, br
  5. Accept: */*
  6. Connection: keep-alive
  7. token: tokenstring
  8. Content-Length: 176
  9. Content-Type: multipart/form-data; boundary=2202e29dea10e9ab00dcf55c67ed1817
  10. --2202e29dea10e9ab00dcf55c67ed1817
  11. Content-Disposition: form-data; name="Smiles"; filename="tmg"
  12. CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl
  13. --2202e29dea10e9ab00dcf55c67ed1817--

使用 httr2 / curl 库,请求应该类似于以下内容:

  1. library(httr2)
  2. getPKA <- function(smi){
  3. upload_url <- "site name"
  4. # 似乎需要从磁盘读取实际文件以包括 filename="tmg"
  5. tmg_path <- file.path(tempdir(), "tmg")
  6. write(smi, tmg_path)
  7. tmg_form_data <- curl::form_file(tmg_path, type = "text/plain")
  8. request(upload_url) %>%
  9. req_headers(token = "tokenstring") %>%
  10. req_body_multipart(Smiles = tmg_form_data) %>%
  11. req_timeout(5) %>%
  12. req_perform(verbosity = 2) %>%
  13. resp_body_json()
  14. }
  15. getPKA("CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl")
  16. # -> POST / HTTP/1.1
  17. # -> Host: localhost:1234
  18. # -> User-Agent: httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0
  19. # -> Accept: */*
  20. # -> Accept-Encoding: deflate, gzip
  21. # -> token: tokenstring
  22. # -> Content-Length: 220
  23. # -> Content-Type: multipart/form-data; boundary=------------------------744c5a08426eb63b
  24. # ->
  25. # >> --------------------------744c5a08426eb63b
  26. # >> Content-Disposition: form-data; name="Smiles"; filename="tmg"
  27. # >> Content-Type: text/plain
  28. # >>
  29. # >> CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl
  30. # >>
  31. # >> --------------------------744c5a08426eb63b--
  32. # Error:
  33. # ! Timeout was reached: [localhost:1234] Operation timed out after 5001 milliseconds with 0 bytes received

这是你提供的代码和请求的翻译。

英文:

That requests call POSTs a multipart-encoded file and request looks something like this:

  1. POST / HTTP/1.1
  2. Host: localhost:1234
  3. User-Agent: python-requests/2.28.2
  4. Accept-Encoding: gzip, deflate, br
  5. Accept: */*
  6. Connection: keep-alive
  7. token: tokenstring
  8. Content-Length: 176
  9. Content-Type: multipart/form-data; boundary=2202e29dea10e9ab00dcf55c67ed1817
  10. --2202e29dea10e9ab00dcf55c67ed1817
  11. Content-Disposition: form-data; name=&quot;Smiles&quot;; filename=&quot;tmg&quot;
  12. CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl
  13. --2202e29dea10e9ab00dcf55c67ed1817--

With httr2 / curl, this should be close enough:

  1. library(httr2)
  2. getPKA &lt;- function(smi){
  3. upload_url &lt;- &quot;site name&quot;
  4. # Seems that we need to read actual file from disk to include filename=&quot;tmg&quot;
  5. tmg_path &lt;- file.path(tempdir(),&quot;tmg&quot;)
  6. write(smi,tmg_path)
  7. tmg_form_data &lt;- curl::form_file(tmg_path, type = &quot;text/plain&quot;)
  8. request(upload_url) %&gt;%
  9. req_headers(token = &quot;tokenstring&quot;) %&gt;%
  10. req_body_multipart(Smiles = tmg_form_data) %&gt;%
  11. req_timeout(5) %&gt;%
  12. req_perform(verbosity = 2) %&gt;%
  13. resp_body_json()
  14. }
  15. getPKA(&quot;CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl&quot;)
  16. #&gt; -&gt; POST / HTTP/1.1
  17. #&gt; -&gt; Host: localhost:1234
  18. #&gt; -&gt; User-Agent: httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0
  19. #&gt; -&gt; Accept: */*
  20. #&gt; -&gt; Accept-Encoding: deflate, gzip
  21. #&gt; -&gt; token: tokenstring
  22. #&gt; -&gt; Content-Length: 220
  23. #&gt; -&gt; Content-Type: multipart/form-data; boundary=------------------------744c5a08426eb63b
  24. #&gt; -&gt;
  25. #&gt; &gt;&gt; --------------------------744c5a08426eb63b
  26. #&gt; &gt;&gt; Content-Disposition: form-data; name=&quot;Smiles&quot;; filename=&quot;tmg&quot;
  27. #&gt; &gt;&gt; Content-Type: text/plain
  28. #&gt; &gt;&gt;
  29. #&gt; &gt;&gt; CCOP(=S)(OCC)OC1=NC(=C(C=C1Cl)Cl)Cl
  30. #&gt; &gt;&gt;
  31. #&gt; &gt;&gt; --------------------------744c5a08426eb63b--
  32. #&gt; Error:
  33. #&gt; ! Timeout was reached: [localhost:1234] Operation timed out after 5001 milliseconds with 0 bytes received

<sup>Created on 2023-05-13 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月13日 20:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定