import socket
import sys
import binascii
import datetime
import time
try:
port = 2122
host = ''
except IndexError:
print ("Error: ")
print ("Be sure to specify username, password, port, database name and table name.\n")
print ("Syntax: ./script.py username password port db_name table_name.")
print ("Example: ./script.py hank mypass 8762 testdb test_table")
print ("\nExit...")
sys.exit(1)
while 1:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
c, addr = s.accept()
print ('Connected by', addr)
data = c.recv(1042)
s=binascii.hexlify(data)
print(s)
data = None
c.close()
time.sleep(1)
print('Exited!')
以上是我在无限循环中的编码,以保持监听客户端,等待客户端发送数据并打印它。就这样。这种写法会不会导致内存泛滥?因为保持打开套接字并以无限循环关闭!。
P/S:我试过一次绑定和监听套接字,只用.recv循环,但它不起作用。只能接收一次数据。