#import socket module from socket import * serverSocket = socket(AF_INET, SOCK_STREAM) #Prepare a sever socket serverPort = 8081 serverSocket.bind(('', serverPort)) serverSocket.listen(1)
whileTrue: #Establish the connection print('Ready to serve...') connectionSocket, addr = serverSocket.accept() try: message = connectionSocket.recv(1024) filename = message.split()[1] f = open(filename[1:]) outputdata = f.read()
#Send one HTTP header line into socket header = ' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' % (len(outputdata)) connectionSocket.send(header.encode())
#Send the content of the requested file to the client for i inrange(0, len(outputdata)): connectionSocket.send(outputdata[i].encode()) connectionSocket.close() except IOError: #Send response message for file not found header = 'HTTP/1.1 404 Not Found' connectionSocket.send(header.encode()) #Close client socket connectionSocket.close() serverSocket.close()
# UDPPingerServer.py # We will need the following module to generate randomized lost packets import random from socket import * import random
# Create a UDP socket # Notice the use of SOCK_DGRAM for UDP packets serverSocket = socket(AF_INET, SOCK_DGRAM) # Assign IP address and port number to socket serverSocket.bind(('', 12000))
whileTrue: # Generate random number in the range of 0 to 10 rand = random.randint(0, 10) # Receive the client packet along with the address it is coming from message, address = serverSocket.recvfrom(1024) # Capitalize the message from the client message = message.upper() # If rand is less is than 4, we consider the packet lost and do not respond if rand < 4: continue # Otherwise, the server responds serverSocket.sendto(message, address)
# Mail content subject = "I love computer networks!" contenttype = "text/plain" msg = "I love computer networks!" endmsg = "\r\n.\r\n"
# Choose a mail server (e.g. Google mail server) and call it mailserver mailserver = "smtp.163.com"
# Sender and reciever fromaddress = "******@163.com" toaddress = "******@qq.com"
# Auth information (Encode with base64) username = "******" password = "******"
# Create socket called clientSocket and establish a TCP connection with mailserver clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((mailserver, 25))
recv = clientSocket.recv(1024).decode() print(recv) if recv[:3] != '220': print('220 reply not received from server.')
# Send HELO command and print server response. heloCommand = 'HELO Alice\r\n' clientSocket.send(heloCommand.encode()) recv1 = clientSocket.recv(1024).decode() print(recv1) if recv1[:3] != '250': print('250 reply not received from server.')
# Auth clientSocket.sendall('AUTH LOGIN\r\n'.encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '334'): print('334 reply not received from server')
clientSocket.sendall((username + '\r\n').encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '334'): print('334 reply not received from server')
clientSocket.sendall((password + '\r\n').encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '235'): print('235 reply not received from server')
# Send MAIL FROM command and print server response. clientSocket.sendall(('MAIL FROM: <' + fromaddress + '>\r\n').encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '250'): print('250 reply not received from server')
# Send RCPT TO command and print server response. clientSocket.sendall(('RCPT TO: <' + toaddress + '>\r\n').encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '250'): print('250 reply not received from server')
# Send DATA command and print server response. clientSocket.send('DATA\r\n'.encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '354'): print('354 reply not received from server')
# Message ends with a single period. clientSocket.sendall(endmsg.encode()) recv = clientSocket.recv(1024).decode() print(recv) if (recv[:3] != '250'): print('250 reply not received from server')
# Send QUIT command and get server response. clientSocket.sendall('QUIT\r\n'.encode())
# 缓存中存在该文件,把它向客户端发送 for i inrange(0, len(outputdata)): tcpCliSock.send(outputdata[i].encode()) print('Read from cache')
# 缓存中不存在该文件,异常处理 except IOError: print('File Exist: ', fileExist) if fileExist == "false": # 在代理服务器上创建一个tcp socket print('Creating socket on proxyserver') c = socket(AF_INET, SOCK_STREAM)
hostn = message.split()[1].partition("//")[2].partition("/")[0] print('Host Name: ', hostn) try: # 连接到远程服务器80端口 c.connect((hostn, 80)) print('Socket connected to port 80 of the host')
c.sendall(message.encode()) # Read the response into buffer buff = c.recv(4096)
tcpCliSock.sendall(buff) # Create a new file in the cache for the requested file. # Also send the response in the buffer to client socket # and the corresponding file in the cache tmpFile = open("./" + filename, "w") tmpFile.writelines(buff.decode().replace('\r\n', '\n')) tmpFile.close()
except: print("Illegal request")
else: # HTTP response message for file not found # Do stuff here print('File Not Found...Stupid Andy') # Close the client and the server sockets tcpCliSock.close() tcpSerSock.close()