Python使用封裝類協程接收客戶端資料,卡在接收函式,進不去
阿新 • • 發佈:2019-01-06
這是由於模組名衝突導致的,使用了from socket import * ,應該用import socket 使用socket.socket去建立套接字等
from socket import *
import sysimport gevent
from gevent import monkey
monkey.patch_all()
class HTTPServer(object):
def __init__(self, port):
self.server_socket = socket(AF_INET, SOCK_STREAM)
self.server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.server_socket.bind(("192.168.46.23", port))
self.server_socket.listen(128)
# def accept(self):
# self.client_socket, client_addr = self.server_socket.accept()
# print('接受來自客戶端%s的連線請求' % str(client_addr))
# print(self.client_socket.recv(1024))
def deal_recv(self, client_socket):
print('-------------------')
while True:
request = client_socket.recv(1024).decode()
print(request)
def start_forever(self):
while True:
# self.accept()
new_socket, client_addr = self.server_socket.accept()
print('接受來自客戶端%s的連線請求' % str(client_addr))
gevent.spawn(self.deal_recv, new_socket)
# gevent.sleep(0.000011)
print("123123")
# self.deal_recv(new_socket)
# self.send_info()
def send_info(self):
pass
def main():
my_server = HTTPServer(8080)
my_server.start_forever()
if __name__ == "__main__":
main()