Method Not Allowed和Forbidden错误
来源:互联网 发布:北京好吃的餐厅知乎 编辑:程序博客网 时间:2024/06/06 16:36
(1) 在运行下面的代码时,服务器返回 Method Not Allowed,由于对于构造Request时,第二个参数不清楚写什么,因而随便写了个值,对于许多网站,这都没有问题,但对于代码中的网站缺异常了。
# create a requesturl="http://www.dy2018.com/"user_agent="Mozilla/5.0 (Windows NT 5.1; rv:44.0) Gecko/20100101 Firefox/44.0"ref="http://www.dy2018.com/"values={}data=urllib.urlencode(values)headers={'User-Agent':user_agent,'Referer':ref}request=urllib2.Request(url,data,headers)# build a connection and send the request,then get the responsetry: response=urllib2.urlopen(request)except urllib2.URLError, e: print e.reasonelse:# show response print response.read()
既然怀疑问题出在这里,则将代码修改为:
request=urllib2.Request(url)删除第二个和第三个参数后,可以正常获取到内容了。不要随便传递POST data参数!!
此外,在CSDN上搜到一篇文章,对此错误有比较详细的介绍: HTTP 405 错误--Method not allowed
(2) 修改上述代码如下,再次运行,服务器返回: Forbidden
# create a requesturl="http://xueqiu.com/"user_agent="Mozilla/5.0 (Windows NT 5.1; rv:44.0) Gecko/20100101 Firefox/44.0"ref="http://www.dy2018.com/"values={}data=urllib.urlencode(values)headers={'User-Agent':user_agent}request=urllib2.Request(url)根据以前的经验,因为header验证不过导致,因而修改代码为:
request=urllib2.Request(url,data,headers)然而服务器却返回错误: Not Found
怀疑data出问题,进而修改代码如下:
request=urllib2.Request(url,headers=headers)
不要随便传递POST data参数!!
最后,对于参数带有默认值的函数,在调用它时,对其参数的赋值是灵活多样的,仅仅赋值没有默认值的可以;参数顺序改变也可以。
0 0
- Method Not Allowed和Forbidden错误
- HTTP 405 错误--Method not allowed
- HTTP错误405-Method Not Allowed
- 一些错误和异常的解决方法02_@Override is not allowed when implementing interface method
- Method not allowed
- 405 Method Not Allowed
- 405 Method Not Allowed
- Method Not Allowed
- 405 (Method Not Allowed)
- 405 Method Not Allowed
- Method Not Allowed 405
- 关于TOMCAT6+COMET出现405 Method Not Allowed错误解决方法
- SVN报MKCOL request for ... 405 Method Not Allowed错误
- iis 405 Method Not Allowed
- HTTP 405Method not allowed
- webapi 405 method not allowed
- post 405 method not allowed
- 在input中回车后页面提交导致出现HTTP 错误 405.0 - Method Not Allowed
- Hibernate映射集合属性
- 线程的创建和启动
- log4j配置和使用
- Android四大组件之Service
- STM32 HAL I2C库读指定寄存器的数据
- Method Not Allowed和Forbidden错误
- hdoj 2084 数塔(经典dp)
- HUST 1602 Substring
- JAVA-内部类
- UNP daytime例子报文分析
- 初遇
- 第二周求圆周率
- JAVA web调用执行python脚本程序的四种方式,回避java.lang.OutOfMemoryError:PermGen space内存溢出问题
- 第一个NDK小程序之两个数的加法