小编use*_*854的帖子

使用python以错误的顺序拉出目录中的文件

我正在使用目录中的文件填充tkinter列表框.文件的名称都以01 - n中的数字开头.当我查看目录中的文件时,它们按数字顺序出现.但是,当我将文件加载到列表框中时,它们不是按数字顺序排序的.我可以更改前导数字,但相同的文件将始终出现在同一位置.

我只是使用简化的项目名称来简化这个例子.它仍然表明它们不是按字母顺序或数字顺序排序的.

该列表应在我的列表框中显示如下

01. itemA
02. itemB
03. itemC
04. itemD
Run Code Online (Sandbox Code Playgroud)

但它看起来像:

01. itemA
04. itemD
02. itemB
03. itemC
Run Code Online (Sandbox Code Playgroud)

我可以更改前导数字,但文件将始终以相同的顺序(按名称而不是数字)填充.奇怪的是,它甚至不是按字母顺序排列的.

我用过这个

i = 0
for filename in os.listdir(directory):
      fileList.insert(i, filename)
      i = i + 1
Run Code Online (Sandbox Code Playgroud)

还有这个

 for filename in os.listdir(directory):
        fileList.insert(END, filename)
Run Code Online (Sandbox Code Playgroud)

两者都导致相同的事情.

python tkinter list

17
推荐指数
2
解决办法
2万
查看次数

.py文件在浏览器中显示代码而不是运行

我正在尝试使用Python,但无法为localhost正确设置我的服务器(使用Ampps).Python通过IDLE和命令行运行得很好,但是,当我在浏览器中打开文件时,代码会显示而不会运行.

我按照这个http://www.imladris.com/Scripts/PythonForWindows.html教程来设置cgi,但它不起作用.

这是我的"hello world"程序的代码,如果这有什么不同的话.

#!/usr/bin/env python
# -*#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'
Run Code Online (Sandbox Code Playgroud)

有什么建议?

python cgi localhost

13
推荐指数
1
解决办法
2万
查看次数

POST数据未出现在CakePHP控制器中

我在knockout.js表单上使用AJAX来发布CakePHP应该收到的一些信息,但是,Cake似乎找不到任何东西.此外,尽管来自POST的200状态(OK),警报仍未出现.

这是AJAX

$.ajax({  
          url: "/orders/finalize_payment",  
          type: "POST",  
          dataType: "json",  
          contentType: "json",  
          data: JSON.stringify({"customer": customer_id}),  
          success: function(){              
            alert("success");  
          }
    }); 
Run Code Online (Sandbox Code Playgroud)

这是订单控制器中的相应操作.现在,我完全将它剥离到最低限度.

function finalize_payment($id = null){
    $this->layout = false;
    $this->autoRender = false;
    if($this->request->is('post')){ //the user has submitted which status to view
        print_r($this->request->data);
            echo "test"; //just to make sure it's reaching this point
    }
}
Run Code Online (Sandbox Code Playgroud)

当我打开chrome中的网络选项卡时,它会将请求有效负载显示为

customer: 1
Run Code Online (Sandbox Code Playgroud)

POST显示为成功,状态为200.我检查了响应标题,它只是显示

array
(
)
test
Run Code Online (Sandbox Code Playgroud)

尽管chrome显示有效载荷被发送,但CakePHP显然没有找到它.

更新

我将请求从AJAX更改为$ .post并且它有效.我仍然不知道为什么

$.post("/orders/finalize_payment",{"customer_id":customer_id},function(data){
        alert('success');
 });
Run Code Online (Sandbox Code Playgroud)

php ajax jquery cakephp

11
推荐指数
2
解决办法
1万
查看次数

对于使用PHP的简单应用程序,Websocket优于AJAX

我已经使用了一些带有PHP的AJAX来提交表单,我最近开始研究websockets.我按照本教程了解基础知识.从我收集的信息来看,websockets保持连接打开,而AJAX打开并关闭请求.

我的问题是如果你只是提交表单或简单的任务,比如auto_complete(无论如何都有一个jQuery插件),websockets提供了优于AJAX的任何优势吗?也许这个教程并不是最好的,但似乎有很多代码可以让websockets工作(至少用PHP),而不仅仅是一个简单的AJAX调用(或者使用捆绑它的jQuery).我在一些地方读到它有点快,但如果我正在处理一些没有收到大量请求的东西,它真的会有所作为吗?如果我错了,请纠正我,但并非所有浏览器都支持websockets,对吧?

php ajax jquery websocket

7
推荐指数
2
解决办法
1万
查看次数

用PHP实现OpenID

我对实现OpenID感兴趣并且我一直在阅读它,但仍然有一些方面我有点困惑.

我已经看到了交互和逐步详细信息的多个流程图,例如这个,但它们都跳过了有关成功登录后会发生什么的详细信息.我读过的所有内容都说"成功登录后,用户会被重定向回网站".那么,我的网站如何知道登录成功了?是否设置了Cookie,我还会收到回复邮件吗?

例如,以下是我所包含的链接的详细信息

9. User POSTs response to OpenID Server.
10. User is redirected to either the success URL or the failure URL returned in (5) depending on the User response

//this is the step that it says tells me I've had a succes/failure upon login
5. Consumer inspects the HTML document header for <link/> tags with the attribute rel set to openid.server and, optionally, openid.delegate. The Consumer uses the values in these tags to construct a URL …
Run Code Online (Sandbox Code Playgroud)

php openid

7
推荐指数
1
解决办法
2265
查看次数

REST的基础知识与php

我正在尝试学习REST的基础知识,我发现了一个非常好的教程(至少它帮助我理解了基础知识).这是我一直关注的教程.

无论如何,在这段代码中,作者展示了网站如何使用类似内容的基础知识www.example.com/restaurant/42 instead of /?restaurant_ID=42.

任何人都可以解释为什么使用它

explode("/", $path, 2); 
instead of
explode("/", $path);
Run Code Online (Sandbox Code Playgroud)

对于这个例子,他们会生成相同的数组,但如果它是一个更长的路径,如果Restaurant/item/3?你不想分开一切吗?正如您所看到的,在这个区块中,他们确实使用了爆炸而没有定义限制.第一个仅仅是资源吗?(我猜控制器是否是MVC)

<?php
// assume autoloader available and configured
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$path = trim($path, "/");
@list($resource, $params) = explode("/", $path, 2); //why is limit used here?

$resource = ucfirst(strtolower($resource));
$method = strtolower($_SERVER["REQUEST_METHOD"]);
$params = !empty($params) ? explode("/", $params) : array();  //no limit here?

if (class_exists($resource)) {
 try {
    $resource = new $resource($params);
    $resource->{$method}();
}
catch (Exception $e) {
    header("HTTP/1.1 500 …
Run Code Online (Sandbox Code Playgroud)

php rest

6
推荐指数
1
解决办法
236
查看次数

使用 nginx 反向代理运行多个 docker-compose 文件

在这里问了一个问题并解决了我的部分问题,但我被建议创建另一个问题,因为它在评论中开始变得有点冗长。

我正在尝试使用 docker 在我的 Mac 上运行多个基于 PHP、MySQL 和 Apache 的应用程序,所有这些应用程序都将使用不同的docker-compose.yml文件(我链接的帖子中有更多详细信息)。我有很多存储库,其中一些存储库相互通信,而且并非所有存储库都是相同的 PHP 版本。正因为如此,我认为将 20 多个单独的存储库塞进一个 docker-compose.yml 文件中对我来说是不明智的。我想为每个存储库使用单独的 docker-compose.yml 文件,并且我希望能够/etc/hosts为每个应用程序使用一个条目,这样我就不必指定端口。例如:我会访问 2 个不同的存储库,例如http://dockertest.comhttp://dockertest2.com(使用/etc/hosts条目),而不必像http://dockertest.com:8080和一样指定端口http://dockertest.com:8081

使用我另一篇文章中接受的答案,我能够一次运行一个应用程序(一个 docker-compose.yml 文件),但是如果我尝试用docker-compose up -d它启动另一个应用程序会导致错误,因为端口 80 已经被占用。如何同时运行多个 docker 应用程序,每个应用程序都有自己的docker-compose.yml文件,而不必在 url 中指定端口?

这是我制作的应用程序的 docker-compose.yml 文件。在我的/etc/hosts我有127.0.0.1 dockertest.com

version: "3.3"
services:
  php:
    build: './php/'
    networks:
      - backend
    volumes:
      - ./public_html/:/var/www/html/
  apache:
    build: './apache/'
    depends_on:
      - php
      - mysql
    networks:
      - …
Run Code Online (Sandbox Code Playgroud)

apache nginx docker

6
推荐指数
1
解决办法
3321
查看次数

在数组中的谷歌地图上放置多个标记

我正在尝试在地图上放置多个标记,这些标记是从数组中提供的.现在只有我的初始点加载(NYC).

var geocoder;
var map;
var markersArray = [];

//plot initial point using geocode instead of coordinates (works just fine)
  function initialize() {
    geocoder = new google.maps.Geocoder();
     latlang = geocoder.geocode( { 'address': 'New York City'}, function(results, status) { //use latlang to enter city instead of coordinates 
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
                });
            markersArray.push(marker);
            }
            else{
            alert("Geocode was not successful for the following reason: " + status);
            }
        });
    var myOptions = …
Run Code Online (Sandbox Code Playgroud)

javascript google-maps geocode

5
推荐指数
1
解决办法
5720
查看次数

python的空白问题

我目前在Windows 7中使用Komodo Edit,但是,我在使用TextWrangler的Mac上遇到过这个问题.无论出于何种原因,当我用Python编写时,我遇到了一些空白错误,这是一个很大的问题.例如,一切看起来都是正确的标签,但Komodo目前给我一个"模糊的空白"错误

//How it appears in my editor
def SaveList(self, directory):
    templist = []
    templistbox2 = []
    for n,i in enumerate(self.listbox2.get(0,END)): 
       templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))
    for filename in sorted(os.listdir(directory)):
        self.templist.insert(i, filename)
        print filename #whitespace error here
Run Code Online (Sandbox Code Playgroud)

考虑到我在Windows和Mac上都经历过两个不同的编辑器,我想知道是否有一些我不知道的设置,或者我是否做错了什么.

python whitespace komodo

4
推荐指数
1
解决办法
3988
查看次数

jquery事件处理链接

我开始使用jquery并按照此处的官方网站上的教程进行操作. http://docs.jquery.com/How_jQuery_Works#jQuery:_The_Basics

我在文档就绪中标记为启动代码的部分中.如果你注意到,它们提供了两个例子.在将您带到jquery站点之前会弹出一个警告框,另一个警告框会阻止您进入该站点.

假设我想要两个链接.一个出现警告框,然后单击"确定",然后进入jquery的站点,另一个出现警告框,但阻止你进入jquery的站点.我只是希望能够找出不同链接的不同响应.我需要给它一些身份证吗?

这是代码.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
<title>Demo</title>
  </head>
  <body>
<script src="jquery.js"></script><br/>
<a href="http://jquery.com/" id="1">jQuery</a><br/> <!--first link: will display message and then proceed to site -->
<script>
 $(document).ready(function(){
    $("a#1").click(function(event){
      alert("Thanks for visiting!");
    });
  });
</script>
<a href="http://jquery.com/" id="2">jQuery</a> <!-- second link: message appears and does not continue to site -->
<script>
   $(document).ready(function(){
      $("a#2").click(function(event){
        alert("As you can see, the link no longer took you to jquery.com"); 
        event.preventDefault();
      });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

编辑 - 将id添加到锚点.谢谢你们,它的确有效.

javascript jquery

3
推荐指数
2
解决办法
8291
查看次数