当从python中的类调用时,HTTP请求不起作用

2024-04-23 08:59:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类a,它有一个posit()方法。 课程名称:测试.py你知道吗

class A():
   def postit(self, D):
     try:
       '''
       request = urllib2.Request("localhost:8079" + '/check/hello-test', D, headers)
       response = urllib2.urlopen(request)

       '''

      conn = httplib.HTTPConnection("http://xxx.xxx.x.x:8079")
      conn.request("GET", "/check/hello-test")
      response = conn.getresponse()
      print ("Whey")   
     except:
      print("Failed to connect!" ) 

有一个服务器正在侦听上述端口。 当我将未注释的代码作为python代码运行时,脚本会向服务器发送请求,但当我执行from test import A操作时,会调用posit函数。它失败了,我得到了返回的错误。你知道吗

上面的posit()函数是为某个目的在内部调用的,它无法发送请求并获取响应。我要知道为什么。你知道吗

我也尝试过删除D参数,但仍然失败。你知道吗


Tags: 方法函数pytesthelloresponserequestcheck