0

我想尝试为我的服务器端创建一个客户端(通过以太网协议发送原始数据)但我有这个错误:

File "/usr/lib/python2.7/socket.py", line 206, in accept
sock, addr = self._sock.accept()

socket.error: [Errno 95] 不支持操作

虽然我这样称呼:sudo python server.py

这是我的 server.py 代码:

  from socket import *
server= socket(AF_PACKET,SOCK_RAW)

host='eth0'
port=4096

address=(host,port)
server.bind(address)

print("started listening " + host + ": " ,port)
client,addr=server.accept()
print("Got a connection from " + addr[0], ": ",addr[1])
while True:
    data =client.recv(4096)
    print("Received" + data + " from the client" )
    print ("proccesing data")
    if(data == "Hello server"):
        client.send(("Hello client").encode('utf-8'))
        print("  Proccesing done. \n   Reply Send")
    elif(data =="disconnect"):
        client.send(("Goodbye").encode('utf-8'))
        client.close()
        break
    else:
        client.send((" Invalid data ").encode('utf-8'))
        print("  Proccessing done Invalid data . \n Reply send ")
4

1 回答 1

0

根据 Sam Maso 的评论,这是工作。我希望这对其他人有帮助。

from socket import *
server= socket(AF_PACKET,SOCK_RAW)

host='eth0'
port=4096

address=(host,port)
server.bind(address)

print("started listening " + host + ": " ,port)


while True:
    data =server.recv(4096)
    print("Received" + data + " from the client" )
    print ("proccesing data")
    if(data == "Hello server"):
        server.send(("Hello client").encode('utf-8'))
        print("  Proccesing done. \n   Reply Send")
    elif(data =="disconnect"):
        server.send(("Goodbye").encode('utf-8'))
        server.close()
        break
    else:
        server.send((" Invalid data ").encode('utf-8'))
        print("  Proccessing done Invalid data . \n Reply send ")
于 2018-09-26T14:42:19.597 回答