Python 在uwsgi下,如何使用urllib2.urlopen连接到其他服务?
我正在使用nginx+uwsgi+烧瓶。在flask中,我需要使用urllib2.urlopen访问google翻译API。我只需运行flask的.py文件就可以得到正确的结果。但一旦我连接到uWSGI,它总是有分段错误 我的uWSGI.ini:Python 在uwsgi下,如何使用urllib2.urlopen连接到其他服务?,python,nginx,segmentation-fault,urllib2,uwsgi,Python,Nginx,Segmentation Fault,Urllib2,Uwsgi,我正在使用nginx+uwsgi+烧瓶。在flask中,我需要使用urllib2.urlopen访问google翻译API。我只需运行flask的.py文件就可以得到正确的结果。但一旦我连接到uWSGI,它总是有分段错误 我的uWSGI.ini: [uwsgi] socket = 127.0.0.1:10001 chdir = /Users/xingshi/Workspace/misc/howToSpeak/flask/py wsgi-file = api.py pyargv = ../con
[uwsgi]
socket = 127.0.0.1:10001
chdir = /Users/xingshi/Workspace/misc/howToSpeak/flask/py
wsgi-file = api.py
pyargv = ../config/mac.cfg
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:10002
master = true
harakiri = 60
调用urllib2的Python代码段
query = urllib.urlencode(fields)
url = 'https://www.googleapis.com/language/translate/v2?'+query
print url
json_str = urllib2.urlopen(url).read()
print json_str
分段错误:
https://www.googleapis.com/language/translate/v2?q=%E6%88%91%E7%88%B1%E4%BD%A0&source=zh&target=en&key=AIzaSyADlrkxuZ47AGABAmqk8JsIHPkU_3WS4Wg&format=text
!!! uWSGI process 32069 got Segmentation Fault !!!
*** backtrace of 32069 ***
0 uwsgi 0x000000010d0664e0 uwsgi_backtrace + 48
1 uwsgi 0x000000010d066a13 uwsgi_segfault + 51
2 libsystem_platform.dylib 0x00007fff89f785aa _sigtramp + 26
3 CoreFoundation 0x00007fff8799b6e9 CFRelease + 1097
4 CoreFoundation 0x00007fff879c36f1 withDaemonConnection + 273
5 CoreFoundation 0x00007fff879c316b -[CFPrefsPlistSource alreadylocked_synchronize] + 587
6 CoreFoundation 0x00007fff879c2e63 _copyValueForKey + 131
7 CoreFoundation 0x00007fff879c2db7 -[CFPrefsPlistSource copyValueForKey:] + 71
8 CoreFoundation 0x00007fff879c2c15 -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 149
9 CoreFoundation 0x00007fff879c2b4f -[CFPrefsSource copyValueForKey:] + 79
10 CoreFoundation 0x00007fff879c2ae0 __CFPreferencesCopyAppValue_block_invoke + 32
11 CoreFoundation 0x00007fff879bccbe +[CFPrefsSearchListSource withSearchListForIdentifier:perform:] + 846
12 CoreFoundation 0x00007fff879bc928 CFPreferencesCopyAppValue + 168
13 SystemConfiguration 0x00007fff8e6de76e SCDynamicStoreCopyProxiesWithOptions + 153
14 SystemConfiguration 0x00007fff8e6de6c0 SCDynamicStoreCopyProxies + 27
15 _scproxy.so 0x000000010dbbeb9e get_proxies + 14
16 libpython2.7.dylib 0x000000010d6351ef PyEval_EvalFrameEx + 12338
17 libpython2.7.dylib 0x000000010d638864 _PyEval_SliceIndex + 829
18 libpython2.7.dylib 0x000000010d6354d4 PyEval_EvalFrameEx + 13079
19 libpython2.7.dylib 0x000000010d638864 _PyEval_SliceIndex + 829
20 libpython2.7.dylib 0x000000010d6354d4 PyEval_EvalFrameEx + 13079
21 libpython2.7.dylib 0x000000010d632093 PyEval_EvalCodeEx + 1641
22 libpython2.7.dylib 0x000000010d5d9796 PyFunction_SetClosure + 809
23 libpython2.7.dylib 0x000000010d5bbf72 PyObject_Call + 101
24 libpython2.7.dylib 0x000000010d5c69a7 PyMethod_New + 1200
25 libpython2.7.dylib 0x000000010d5bbf72 PyObject_Call + 101
只需使用urllib3
解决方法:
使用no_proxy='*'运行代码似乎可以阻止它尝试加载代理设置。能否引用源代码?由于链接腐烂,不鼓励简单地链接到答案。
url = 'https://www.googleapis.com/language/translate/v2?'+query
http = urllib3.PoolManager()
r = http.request('GET',url)
json_str = r.data