-2

当我想运行此代码时出现以下错误。我犯了一个错误我不明白在哪里一切正常

你认为错误在哪里

import socket,time
import thread

    class http():

        def __init__(self):
            self.socket = socket
            self.port = 5000
            self.buffsize = 1024
            self.listen = 5
            self._header = ("HTTP/1.1 200 OK\r\n"
                            "Content-Type: text/html; charset=utf-8\r\n\r\n")

        def _worker(self,socket,sleep):
            # Client connect for thread worker
            while True:
                time.sleep(sleep)
                client,address = socket.accept()
                data = client.recv(1024)
                if data:
                    client.send(self._header)
                    client.send(data)
                client.close()

        def httpHandler(self):
            # Create Socket For Connection
            try:
                self.socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                self.socket.bind(('127.0.0.1',self.port))
                self.socket.listen(self.listen)
                self.socket.setblocking(False)
            except socket.error as error:
                print error
            finally:
                print "HTTP Server Running - 127.0.0.1:5000"
                self._worker(self.socket,1)

    if __name__ == "__main__":
        application = http()
        application.httpHandler()

当我想在终端上运行时,错误

但怎么能说有问题

HTTP Server Running - 127.0.0.1:5000
Traceback (most recent call last):
  File "/Users/batuhangoksu/http.py", line 44, in <module>
    application.httpHandler()
  File "/Users/batuhangoksu/http.py", line 40, in httpHandler
    self._worker(self.socket,1)
  File "/Users/batuhangoksu/http.py", line 22, in _worker
    client,address = socket.accept()
AttributeError: 'module' object has no attribute 'accept'
4

1 回答 1

2

使用self.socket,而不是socket

            client,address = self.socket.accept()

socket是模块的名称。self.socket是一个socket._socketobject,调用返回的值socket.socket。确实,名为“socket”的东西太多了:)。我建议换成self.socket其他东西来帮助区分这些想法。


调用时还需要保存返回值socket.socket。目前,您有

self.socket = socket

它将实例属性self.socket设置为模块socket。这没有用,因为您已经可以使用普通的 old 访问模块socket。相反,使用类似的东西

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

import multiprocessing as mp
import socket
import time

HOST = ''           # Symbolic name meaning all available interfaces
PORT = 50007        # Arbitrary non-privileged port

def server():
    header = ("HTTP/1.1 200 OK\r\n"
              "Content-Type: text/html; charset=utf-8\r\n\r\n")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(5)
    while True:
        conn, addr = s.accept()
        data = conn.recv(1024)
        if not data: break
        conn.send(header)
        conn.send(data)
        conn.close()

def client():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.send('Hello, world')
    data = s.recv(1024)
    s.close()
    print 'Received', repr(data)

sproc = mp.Process(target = server)
sproc.daemon = True
sproc.start()
while True:
    time.sleep(.5)
    client()
于 2013-09-30T19:17:58.777 回答