我正在使用Python服务器与Python的Requests模块进行http调用.请求调用在常规python shell中没有问题.但是在Flask中,它出现了"引发ConnectionError(e)"的错误.这真的很痛,因为我需要使用python在后端进行url调用,因为我不能使用javascript因为跨域限制.
flask python # cat myapp.py
import json
import requests
from flask import Flask
app = Flask(__name__)
@app.route('/', defaults={'path': ''}, methods=('GET', 'POST'))
@app.route('/<path:path>', methods=('GET', 'POST'))
def catch_all(path):
link = "http://10.44.11.33/api/v1/counts/latest/15"
r = requests.get(link)
return "Hello World"
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")
Run Code Online (Sandbox Code Playgroud)
错误输出:
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request() …Run Code Online (Sandbox Code Playgroud) 我想从一些网页中挑选几行信息.我想(或者我)打开网页,遍历各行,检查每个关键字,找到我想要的信息.
这些页面需要一个会话.
def getpage()
home = 'website'
exstension1 = '/input/page'
extension2 = '/output/page'
indexnumber = '11100'
sess = requests.Session()
getter = sess.get(home+extension1)
payload = {'foo':'bar','indexnumber':indexnumber}
getter = sess.post(home+extension2,data=payload)
return sess
Run Code Online (Sandbox Code Playgroud)
正如我在标题中所说的那样,我需要一个.get()的readlines()方法
a.get(somePage)###Now could I put...###.readlines()
####or
a.get(somePage).text.readlines()###?
###I don't think I want the following, for performance reasons, correct me if I am wrong
F = open(someNewFile,mode='w')
F.write(a.get(somePage).text)
F.close()
F = open(thatFileIJustMade).readlines()###All that just to turn it into a File on which I can use readlines?
Run Code Online (Sandbox Code Playgroud)
谢谢
当我尝试
a.get(somePage).readlines()
Run Code Online (Sandbox Code Playgroud)
我明白了
AttributeError: …Run Code Online (Sandbox Code Playgroud) 为什么当我运行此脚本时,我收到错误"无法导入名称请求"
我已经运行了"pip安装请求"
from bzrlib import branch,requests, json
def x:
url = myURL
data = json.dumps({"one": "two"})
r = requests.post(url, data)
print r.json
Run Code Online (Sandbox Code Playgroud) 我想知道是否存在一种常见的模式,可以重试请求一定次数(由于服务器错误或网络故障而导致失败)。我想出了这个,我愿意在那里找到更好的实现。
cnt=0
while cnt < 3:
try:
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return json.loads(response.text)
except requests.exceptions.RequestException:
continue
cnt += 1
return False
Run Code Online (Sandbox Code Playgroud) 当我向url发出请求时,我相信客户端的ip地址被发送到服务器.在这种情况下,ip地址必须出现在请求标头中,否则我错了?
所以,我说
import requests
resp = requests.get("http://localhost:8000/test")
print resp.request.headers
Run Code Online (Sandbox Code Playgroud)
但是在打印resp.request.headers时我看不到任何ip地址,所以如何查看客户端的ip地址.如果我不能将它视为resp.request.headers的一部分,如果客户端的IP地址不在请求头中,它如何获取客户端的IP地址?
我该如何使用requests?
urllib.urlretrieve('ftp://USER:pass@i.website.net/upload/file.csv', filepath)
Run Code Online (Sandbox Code Playgroud)
如果我尝试做一个requests.get(),我会收到一个错误requests.exceptions.InvalidSchema: No connection adapters were found for 'ftp://.
我在抓取网站时遇到了问题.目的是在某些日子里为伦敦的酒店取得价格.为此,我从booking.com加载以下网址,然后尝试搜索关键字.但由于某种原因,requests.get不会下载完整的网站.例如,下面的URL显示了浏览器中的酒店列表.每个都显示'总计'和价格.但是,在下面的代码中,site.find('Total')表示在字符串中找不到单词'Total',即使它在浏览器中可见.任何有关为什么会发生这种情
import requests
url='http://www.booking.com/searchresults.en-gb.html?label=gen173nr-17CAEoggJCAlhYSDNiBW5vcmVmaFCIAQGYAS64AQTIAQTYAQHoAQH4AQs;sid=1a43e0952558ac0ad0061d5b6523a7bc;dcid=1;checkin_monthday=4;checkin_year_month=2016-2;checkout_monthday=11;checkout_year_month=2016-2;city=-2601889;class_interval=1;csflt=%7B%7D;group_adults=7;group_children=0;highlighted_hotels=1192837;hp_sbox=1;label_click=undef;no_rooms=1;review_score_group=empty;room1=A%2CA%2CA%2CA%2CA%2CA%2CA;sb_price_type=total;score_min=0;si=ai%2Cco%2Cci%2Cre%2Cdi;ss=London;ssafas=1;ssb=empty;ssne=London;ssne_untouched=London&;order=price_for_two'
r=requests.get(url)
site=r.text
site.find('Total')
Run Code Online (Sandbox Code Playgroud) 我已经使用了请求模块,现在我得到了json格式的数据,并且通过这个如何使用Python的相同帮助我写了一个JSON文件,我编写了我的代码,在执行代码时它给了我一个错误Expected a string or buffer,所以我改变了变量传递给解析器到字符串.现在它再次出现了另一个错误.
#Import
import requests
import json
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r.status_code)
got_data_in_json = r.json()
parsed_json = json.loads(str(got_data_in_json))
print(json.dumps(parsed_json, indent=4 ,sort_keys=True))
Run Code Online (Sandbox Code Playgroud)
错误日志:
python requests_post.py
200
Traceback (most recent call last):
File "requests_post.py", line 8, in <module>
parsed_json = json.loads(str(got_data_in_json))
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python请求库发送http GET请求。以下是我的代码。
#!/usr/bin/python3
import requests
import json
URL = some-elkstack-url
datam = {'ayyo' : 'vammo'}
data_json = json.dumps(datam)
payload = {'json_payload': data_json}
header={'Content-Type': 'application/json' }
r = requests.get(url=URL, headers=header, data=datam)
a = r.json()
print('\nResponse: \n')
print(a)
Run Code Online (Sandbox Code Playgroud)
我从服务器返回此HTTP错误。
{'error': {'root_cause': [{'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}], 'type': 'json_parse_exception', 'reason': "Unrecognized token 'ayyo': was expecting ('true', 'false' or 'null')\n at
[Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@74cef381; line: 1, column: 6]"}, 'status': 500}
Run Code Online (Sandbox Code Playgroud)
当我在命令行中使用相同的json数据执行curl时,我可以获得正确的响应。我的代码出了什么问题?
如果使用Python 3.7中的条件,如何仅使用单个if条件而不是两个来重写下面的代码?
with open('demo.csv', 'r') as f, open("Result_csv.csv", 'w+') as out:
for line in f:
if '/tcp' in line:
print(line)
out.write(line)
if '/udp' in line:
print(line)
out.write(line)
Run Code Online (Sandbox Code Playgroud) python ×10
python-requests ×10
http ×2
json ×2
python-3.x ×2
exception ×1
flask ×1
python-2.7 ×1
web-scraping ×1