2013-09-30 60 views
-2

我想運行此代碼時出現以下錯誤。我犯了一個錯誤,我不明白的地方一切正常在使用python編寫的類不明白時出現錯誤

如果你認爲錯誤

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' 
+1

使用不同的變量名本地變量'socket' – karthikr

+0

'self.socket = socket' ...這是荒謬的 – Matthias

+1

@Matthias請好的。 http://blog.stackoverflow.com/2012/07/kicking-off-the-summer-of-love/ – tacaswell

回答

2

問題使用self.socket,不socket

  client,address = self.socket.accept() 

socket是模塊的名稱。 self.socketsocket._socketobject,通過調用socket.socket返回的值。實際上,有太多名爲「socket」的東西:)。 我建議將self.socket更改爲其他內容以幫助區分這些想法。


當您撥打socket.socket時,還需要保存返回值。目前,你有

self.socket = socket 

其設置實例屬性self.socket到模塊socket。這沒什麼用處,因爲您已經可以使用普通舊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() 
相關問題