📜  python 到 c++ 编译器 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:36.470000             🧑  作者: Mango

代码示例1
#import socket module 
from socket import * 
serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
TCP_PORT = 8000
BUFFER_SIZE = 1024
    
serverSocket.bind(('', TCP_PORT))
serverSocket.listen(1)

while True:
    #Establish the connection 
    print 'Ready to serve...' 
    connectionSocket, addr = serverSocket.accept()

    print 'Connection address:', addr

    try:
        message = connectionSocket.recv(BUFFER_SIZE) 
        filename = message.split()[1] 
        f = open(filename[1:]) 
        outputdata = f.read()

        #Send one HTTP header line into socket 
        connectionSocket.send('HTTP/1.0 200 OK\r\n')

        #Send the content of the requested file to the client
        
        for i in range(0, len(outputdata)): 
            connectionSocket.send(outputdata[i])
            
        connectionSocket.close()
    
    except IOError: 
        #Send response message for file not found 
        fail = '''   404   

404 Bruh

hushies!

''' connectionSocket.send('HTTP/1.0 200 OK\r\n'%len(fail)) for q in fail: connectionSocket.send(q) #Close client socket serverSocket.close(