python 相当于 java 中的 HttpClient 和 sql 查询
python equivalent of HttpClient in java wth sql query
我需要一些帮助来将 java 代码翻译成 Python。
我不太熟悉 Java 代码。我有一些遗留 java 代码,我必须在 Python 中重写。基本上,它是关于从服务器读取一些数据。 Java 代码使用带有一些 SQL 查询的 httpClient。我想要在 python 中使用的正确模块,例如 pyodbc、urllib 或请求。
HttpClient httpClient = getThreadSafeClient();
HttpPost httppost = new HttpPost("http://someserver:9997/the_store/query/");
httppost.setEntity(new StringEntity(
"select what_i_need from store.data where date=20190722"));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = null;
entity = response.getEntity();
InputStream is = entity.getContent();
我想知道上面 java 代码的 python 等效代码。谢谢
这就是我在 Python 中使用 requests
的方式
我从我的一个项目中获取这个,我使用了一个 get 请求,但是 post 语法应该是这样的。
如果您需要身份验证,您需要定义 USERNAME
和 PASSWORD
import requests
from requests.auth import HTTPBasicAuth
import time
def send_request(post_url):
print(post_url)
try:
session = requests.Session()
response = session.post(post_url,data=dict(), auth=HTTPBasicAuth(USERNAME, PASSWORD))
if(response.status_code == 200):
return response.text
elif(response.status_code == 429):
print("API exhaust was reached, please wait for 30 seconds")
time.sleep(30)
return send_request(post_url)
else:
print response.status_code
return None
except requests.exceptions.RequestException as e:
print(e)
print("HTTP Request failed!")
return None
注:
data
接受 dict()
,您可以使用 json="your_json_formatted_sql_query"
代替 data=dict()
以 json 格式发送有效载荷
我需要一些帮助来将 java 代码翻译成 Python。
我不太熟悉 Java 代码。我有一些遗留 java 代码,我必须在 Python 中重写。基本上,它是关于从服务器读取一些数据。 Java 代码使用带有一些 SQL 查询的 httpClient。我想要在 python 中使用的正确模块,例如 pyodbc、urllib 或请求。
HttpClient httpClient = getThreadSafeClient();
HttpPost httppost = new HttpPost("http://someserver:9997/the_store/query/");
httppost.setEntity(new StringEntity(
"select what_i_need from store.data where date=20190722"));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = null;
entity = response.getEntity();
InputStream is = entity.getContent();
我想知道上面 java 代码的 python 等效代码。谢谢
这就是我在 Python 中使用 requests
我从我的一个项目中获取这个,我使用了一个 get 请求,但是 post 语法应该是这样的。
如果您需要身份验证,您需要定义 USERNAME
和 PASSWORD
import requests
from requests.auth import HTTPBasicAuth
import time
def send_request(post_url):
print(post_url)
try:
session = requests.Session()
response = session.post(post_url,data=dict(), auth=HTTPBasicAuth(USERNAME, PASSWORD))
if(response.status_code == 200):
return response.text
elif(response.status_code == 429):
print("API exhaust was reached, please wait for 30 seconds")
time.sleep(30)
return send_request(post_url)
else:
print response.status_code
return None
except requests.exceptions.RequestException as e:
print(e)
print("HTTP Request failed!")
return None
注:
data
接受 dict()
,您可以使用 json="your_json_formatted_sql_query"
代替 data=dict()