Python POST无法正常工作(Python POST not working)
我正在尝试将POST数据从python脚本发送到url。 这是代码:
def createperson(self,name=None): SERVER_BASE_ADDR = 'http://localhost:8000/' postdata = urllib.urlencode({'name':name}) req = urllib2.Request(SERVER_BASE_ADDR + 'people/add') fd = urllib2.urlopen(req,postdata) return name
但这会回来
HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
在网址上添加一个人很好。 有任何想法吗?
编辑
服务器端使用django装饰器来处理POST和GET。
@require_http_methods(["GET", "POST"]) def add(request): if request.method == "POST": #do stuff with post data
上面的python代码与在html模板上提交表单不一样吗? 如果它相同那么处理程序不应该以同样的方式处理它?
谢谢
通过使用解决:
c = urllib2.build_opener() postdata = urllib.urlencode({'name':name}) r = c.open(url,postdata)
一种不同的方法,但做的工作。 谢谢你的所有答案。
I am trying to send POST data from a python script to a url. Here is the code:
def createperson(self,name=None): SERVER_BASE_ADDR = 'http://localhost:8000/' postdata = urllib.urlencode({'name':name}) req = urllib2.Request(SERVER_BASE_ADDR + 'people/add') fd = urllib2.urlopen(req,postdata) return name
But this returns
HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
Adding a person on the url is fine. Any ideas?
EDIT
The server side uses django decorators to handle POST and GET.
@require_http_methods(["GET", "POST"]) def add(request): if request.method == "POST": #do stuff with post data
Isn't the above python code the same as submitting a form on a html template? If its the same then shouldn't the handlers handle it the same way?
Thank you
Solved by using:
c = urllib2.build_opener() postdata = urllib.urlencode({'name':name}) r = c.open(url,postdata)
A different approach but does the job. Thank you for all your answers.
原文:https://stackoverflow.com/questions/9230140
最满意答案
ConnectionMultiplexer上的Dispose(...)操作关闭连接并释放其他资源,如与ConnectionMultiplexer对象关联的ServerEndpoint对象。 它不会向Redis发出任何命令。
来源如下: https : //github.com/StackExchange/StackExchange.Redis/blob/master/StackExchange.Redis/StackExchange/Redis/ConnectionMultiplexer.cs
The Dispose(...) operation on the ConnectionMultiplexer closes the connections and releases other resources like ServerEndpoint objects associated with the ConnectionMultiplexer object. it doesn't issue any commands to Redis.
The sources are here: https://github.com/StackExchange/StackExchange.Redis/blob/master/StackExchange.Redis/StackExchange/Redis/ConnectionMultiplexer.cs
相关问答
更多-
ConnectionMultiplexer上的Dispose(...)操作关闭连接并释放其他资源,如与ConnectionMultiplexer对象关联的ServerEndpoint对象。 它不会向Redis发出任何命令。 来源如下: https : //github.com/StackExchange/StackExchange.Redis/blob/master/StackExchange.Redis/StackExchange/Redis/ConnectionMultiplexer.cs The Di ...
-
因为redis需要单独连接交互式命令而不是pub / sub订阅。 如果你没有使用pub / sub,你可以告诉选项禁用SUBSCRIBE命令,在这种情况下我认为第二个连接没有建立。 Because redis requires separate connections for interactive commands versus pub/sub subscriptions. If you aren't using pub/sub, you could tell the options to disabl ...
-
我会说有两种可能性: 1 / django应用程序可能无法连接到您认为连接到的Redis实例,或者您启动的redis-cli客户端不连接到相同的Redis实例。 请注意,在这两种情况下,您都不要使用相同的确切连接机制。 Django使用Unix域套接字,而redis-cli使用TCP环回(默认情况下)。 您可能希望使用相同的插槽路径启动redis-cli,以确保: $ redis-cli -s /tmp/redis_6379.sock 现在,由于您已使用MONITOR命令验证您是否看到Django发送的命 ...
-
对于其他任何偶然发现错误的人来说,原因是与Microsoft.Bcl软件包的版本不匹配。 运行命令Update-Package Microsoft.Bcl进行解析。 请参阅http://blog.marcgravell.com/2014/03/so-i-went-and-wrote-another-redis-client.html?showComment=1405397410965#c8649668071319126908 For anyone else who stumbles on this the ...
-
系统应尝试自动重新连接。 它没有做的是重试你的命令,因为它无法知道在服务器上做了什么和没有做什么(因为:套接字失败;因为它知道所有,它可能已经由redis发送了“ok”响应)。 因此,您不需要处置/重新连接。 您可以通过多路复用器实例上发布的事件监视连接失败/重新连接。 您还可以在数据库上使用.IsConnected()方法(这需要一个用于服务器定位的密钥,但如果您只与一个服务器通信,则可以将任何内容作为密钥传递)。 The system should attempt to reconnect automa ...
-
如何在重启时使redis-server刷新所有数据?(How can I make redis-server flushes all data when it restarts?)[2023-06-26]
查看您正在使用的redis.conf文件: # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" 停止redis-server,如上所述配置它,丢弃.rdb文件,然后启动redis ... -
使用带方式的键方法* 根据文件 redis> MSET one 1 two 2 three 3 four 4 OK redis> KEYS ** 1) "one" 2) "two" 3) "three" 3) "four" Use keys method with pattern * According to the docs redis> MSET one 1 two 2 three 3 four 4 OK redis> KEYS ** 1) "one" 2) "two" 3) "three" 3) " ...
-
无法在SignalR 2.2上指定ConnectionMultiplex。 我在这里打开了一个关于它的问题https://github.com/SignalR/SignalR/issues/3383 我正在为回购创建一个公关。 Damian为里程碑2.2.1标记了这个问题,希望很快就会发布(当然我先发送PR) .U there is not possible to specify a ConnectionMultiplex on SignalR 2.2. I opened an issue about th ...
-
Redis的GetDatabase()方法是否缓存所有数据库表?(Do Redis's GetDatabase() method caches all database tables?)[2022-05-28]
简答: 不,Redis的GetDatabase()方法不会缓存所有数据库表 答案很长: 作为Github上的 Per StackExchange.Redis : 使用redis数据库 访问redis数据库非常简单: IDatabase db = redis.GetDatabase(); 从GetDatabase返回的对象是一个廉价的pass-thru对象,不需要存储。 请注意,redis支持多个数据库(尽管“cluster”不支持这种情况); 这可以在GetDatabase调用中选择性地指定。 此外,如果 ... -
您应该让StackExchange.Redis处理重新连接,而不是自己检查IsConnected。 这是我们推荐的模式: private static Lazy
lazyConnection = new Lazy (() => { return ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,abortConnect=false,ss ...