从HTTP服务器向客户端python发送标志

时间:2017-09-07 13:16:18

标签: python httplib basehttprequesthandler

我有一个用Python编写的基本HTTP服务器客户端,其中服务器有不同的模块(活动)来运行。我希望客户端仅在某个广告系列启动时才与服务器进行互动。我想我可以创建一个标志,其值取决于广告系列,并在连接时将此标志发送给客户端,然后根据标志,客户端 - 服务器可以进行交互。但是我该怎么做?

示例:

#Server
def run(server_class, handler_class):
   server_add = (host, port)
   httpd = server_class(server_add, handler_class)
   httpd.serve_forever()

def campaign():
   c = input("Choose Campaign: \r\n 1. Cam1 \r\n 2. Cam2 \r\n")
   if c == 1:
      flag = "Run Cam1" #Set flag
      #Send flag to client
      run()
      do_cam1() #Call the defined function for Campaign 1
   elif c == 2:
      flag = "Run Cam2"
      #Send flag to client
      run()
      do_cam2()
   else:
      print "Invalid Campaign"


#Client
def client():
   server = ''
   c = httplib.HTTPConnection(server)
   #Read the flag here
   if flag == "Run Cam1":
      #Do Something
   elseif flag == "Run Cam2":
      #Do Something
   else:
      #Throw Some Error
   c.close()

0 个答案:

没有答案