我正在PHP中构建一个脚本来与API交互,并且需要能够解析API给我的HTTP状态代码.在大多数情况下,它提供以下响应之一:
HTTP/1.1 401 Unauthorized
HTTP/1.1 403 Forbidden
HTTP/1.1 404 Not Found
HTTP/1.1 410 Gone
Run Code Online (Sandbox Code Playgroud)
我需要能够识别正在给出的响应,并且,如果它的401或410,继续,但是,如果它是401或403,在连续几次之后跟踪并关闭脚本(因为我超过了当天的通话限制.
我的代码非常简单:
for($i = $start;$i < $end;$i++)
{
// construct the API url
$url = $base_url.$i.$end_url;
// make sure that the file is accessible
if($info = json_decode(file_get_contents($url)))
{
// process retrieved data
} else {
// what do I put here?
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道在'else'循环中放什么.我正在使用CodeIgniter框架,如果有人知道任何使用的快捷方式.此外,我愿意使用cURL,但从未有过.
我正在尝试在这里完成Node.io的教程工作.我运行内置模块没有问题:
echo "mastercard.com" | node.io -s pagerank
Run Code Online (Sandbox Code Playgroud)
要么:
node.io query "http://www.reddit.com/" a.title
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试自己的模块时:
var nodeio= require('node.io');
exports.job = new nodeio.Job({
input: false,
run: function() {
this.emit('Hello World');
}
Run Code Online (Sandbox Code Playgroud)
});
并尝试运行它:
node.io hello
Run Code Online (Sandbox Code Playgroud)
我明白了:
ERROR: Failed to load job "hello". Please check that the job exists and compiles correctly.
Run Code Online (Sandbox Code Playgroud)
调试给了我这个:
DEBUG: {"stack":"Error: Cannot find module 'node.io'\n at Function._resolveFilename (module.js:322:11)\n at Function._load (module.js:267:25)\n at require (module.js:351:19)\n at Object.<anonymous> (/Users/username/Documents/Nodejs/hello.js:1:75)\n at Module._compile (module.js:407:26)\n at Object..js (module.js:413:10)\n at Module.load (module.js:339:31)\n at Function._load …Run Code Online (Sandbox Code Playgroud) 我正在努力将一系列交互式教育应用程序从Flash转换为Javascript,我的团队正在计划使用Backbone.js作为框架.这些应用程序中的每一个基本上是一组场景,其向用户呈现信息和/或以问题或交互式小部件的形式提示某些交互.我们为应用程序考虑的基本结构如下:
当然,问题在于我是Backbone的新手.我已经完成了一些基本的教程,但是在将Backbone与静态JSON文件集成时遇到了问题.
假设我有以下非常基本的JSON文件,它显示了三个要显示的场景:
var scenes = [
{
"name": "Introduction",
"label": "Introduction",
"message": "Welcome to this app"
},
{
"name": "Exercise",
"label": "Exercise",
"message": "If this were a real app, there'd be some sort of exercise here"
},
{
"name": "Conclusion",
"label": "Conclusion",
"order": "Thank you for completing this app"
}
]
Run Code Online (Sandbox Code Playgroud)
我需要的,以及我正在尝试做的是让Backbone生成导航小部件,让用户在这些场景之间导航并显示每个场景的消息.(这显然是真实应用程序的简化版本.)
这是我尝试过的:
// simplified object containing stage information
var stages = [
{
"name": "Introduction",
"label": "Introduction",
"message": "Welcome to this app"
},
{ …Run Code Online (Sandbox Code Playgroud) 我有一个基本的注册表单,其中包括一个BooleanField供人们接受条款和隐私政策的表格。我想要做的是更改用户不检查时引发的 ValidationError 的语言。
class RegisterForm(forms.Form):
username = forms.CharField(label="Username")
email = forms.EmailField(label="Email")
location = forms.CharField(label="Location",required=False)
headline = forms.CharField(label="Headline",required=False)
password = forms.CharField(widget=forms.PasswordInput,label="Password")
confirm_password = forms.CharField(widget=forms.PasswordInput,label="Confirm Password")
terms = TermsField(label=mark_safe("I have read and understand the <a href='/terms'>Terms of Service</a> and <a href='/privacy'>Privacy Policy</a>."),required=True)
Run Code Online (Sandbox Code Playgroud)
TermsField子类来自BooleanField:
class TermsField(forms.BooleanField):
"Check that user agreed, return custom message."
def validate(self,value):
if not value:
raise forms.ValidationError('You must agree to the Terms of Service and Privacy Policy to use this site.')
else:
super(TermsField, self).validate(value)
Run Code Online (Sandbox Code Playgroud)
它可以正确验证,因为如果用户不检查它们的 …
我正在尝试在.asp页面上提交表单,但Mechanize无法识别控件的名称.表单代码是:
<form id="form1" name="frmSearchQuick" method="post">
....
<input type="button" name="btSearchTop" value="SEARCH" class="buttonctl" onClick="uf_Browse('dledir_search_quick.asp');" >
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
br = mechanize.Browser()
br.open(BASE_URL)
br.select_form(name='frmSearchQuick')
resp = br.click(name='btSearchTop')
Run Code Online (Sandbox Code Playgroud)
我也试过最后一行:
resp = br.submit(name='btSearchTop')
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
raise ControlNotFoundError("no control matching "+description) ControlNotFoundError: no control matching name 'btSearchTop', kind 'clickable'
Run Code Online (Sandbox Code Playgroud)
如果我打印br我得到这个:IgnoreControl(btSearchTop =)
但是我没有在HTML中看到任何地方.
有关如何提交此表格的任何建议?
我在PHP中有这个代码:
if($id = $this->ci->srr->check_faca_indiv($name))
{
return $id;
} elseif($id = $this->_addIndiv($name)) {
return $id;
} else {
return null;
Run Code Online (Sandbox Code Playgroud)
我在整个地方使用这种基本结构,它可能不是很好,但它是我习惯的.函数基本上尝试做一些简单的操作,如果失败则返回null值.我试图模仿将其移植到Python,但是语法错误并最终将其修改为:
indiv_id = checkIndiv(name)
if indiv_id != None:
return indiv_id
indiv_id = addIndiv(name)
if indiv_id != None:
return indiv_id
else:
return None
Run Code Online (Sandbox Code Playgroud)
我认为这让我到了同一个地方,但我不喜欢它 - 有没有一种方式更类似于PHP,我错过了?
我正在尝试使用simpleXML在php中解析一个中等大小的XML文件(6mb).该脚本从XML文件中获取每条记录,检查它是否已经被导入,如果没有,则将该记录更新/插入到我自己的数据库中.
问题是我经常遇到超出内存分配的致命错误:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 256 bytes) in /.../system/database/drivers/mysql/mysql_result.php on line 162
Run Code Online (Sandbox Code Playgroud)
我通过使用以下行来增加最大内存分配来避免该错误(从此处开始提示):
ini_set('memory_limit', '-1');
Run Code Online (Sandbox Code Playgroud)
然而,然后我遇到60秒的最大执行时间,并且,无论出于何种原因,我的服务器(Mac OS X上的XAMPP)都不会让我增加那个时间(如果我尝试包含脚本,脚本就不会运行像:)这样的一行
set_time_limit(240);
Run Code Online (Sandbox Code Playgroud)
然而,这一切似乎都非常低效; 我不应该能够打破文件的顺序并按顺序处理它吗?在下面的控制器中,我有一个计数变量($ cycle)来跟踪我所在的记录,但我无法弄清楚如何实现它仍然不需要处理整个XML文件.
控制器(我正在使用CodeIgniter)具有以下基本结构:
$f = base_url().'data/data.xml';
if($data = file_get_contents($f))
{
$cycle = 0;
$xml = new SimpleXMLElement($data);
foreach($xml->person as $p)
{
//this makes a single call to db for single field based on id of record in XML file
if($this->_notImported('source',$p['id']))
{
//various process here, mainly breaking up the data …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个URL列表放入一个csv文件中,我正在使用urllib2和BeautifulSoup从网页上抓取这些文件.我尝试将链接写入csv文件作为unicode,并转换为utf-8.在这两种情况下,每个字母都插入到一个新字段中.
这是我的代码(我至少尝试过这两种方式):
f = open('filename','wb')
w = csv.writer(f,delimiter=',')
for link in links:
w.writerow(link['href'])
Run Code Online (Sandbox Code Playgroud)
和:
f = open('filename','wb')
w = csv.writer(f,delimiter=',')
for link in links:
w.writerow(link['href'].encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
links 是一个如下所示的列表:
[<a href="#Flyout1" accesskey="2" class="quicklinks" tabindex="1" title="Skip to content">Quick Links: Skip to main page content</a>, <a href="#search" class="quicklinks" tabindex="1" title="Skip to search">Skip to Search</a>, <a href="#News" class="quicklinks" tabindex="1" title="Skip to Section table of contents">Skip to Section Content Menu</a>, <a href="#footer" class="quicklinks" tabindex="1" title="Skip to site options">Skip to Common Links</a>, <a href="http://www.hhs.gov"><img src="/ucm/groups/fdagov-public/@system/documents/system/img_fdagov_hhs_gov.png" alt="www.hhs.gov link" …Run Code Online (Sandbox Code Playgroud) php ×3
python ×3
javascript ×2
asp-classic ×1
backbone.js ×1
csv ×1
curl ×1
django ×1
django-forms ×1
if-statement ×1
json ×1
mechanize ×1
node.js ×1
syntax ×1
validation ×1
xml ×1