2012-01-19 11 views
13

में सर्वर पक्ष पर HTTP HTTP इनपुट पैरामीटर प्रोसेसिंग मैंने अनुभव के लिए पाइथन में एक साधारण HTTP क्लाइंट और सर्वर लिखा था। नीचे दिया गया पहला कोड स्निपेट दिखाता है कि मैं HTTP को एक पैरामीटर के साथ अनुरोध कैसे भेजता हूं अर्थात् आईएमआईआई। दूसरे कोड स्निपेट में मैं सर्वर के पक्ष में अपना डूगेट फ़ंक्शन कार्यान्वयन दिखाता हूं। मेरा सवाल यह है कि मैं सर्वर कोड में आईएमएसआई पैरामीटर कैसे निकाल सकता हूं और क्लाइंट को सिग्नल सिग्नल करने के लिए क्लाइंट को प्रतिक्रिया भेज सकता हूं कि आईएमआई वैध है। धन्यवाद।पाइथन

पीएस .: मैंने सत्यापित किया कि क्लाइंट सफलतापूर्वक अनुरोध भेजता है।

ग्राहक कोड स्निपेट

params = urllib.urlencode({'imsi': str(imsi)}) 
    conn = httplib.HTTPConnection(host + ':' + str(port)) 
    #conn.set_debuglevel(1) 
    conn.request("GET", "/index.htm", 'imsi=' + str(imsi)) 
    r = conn.getresponse() 

सर्वर कोड स्निपेट

import sys, string,cStringIO, cgi,time,datetime 
from os import curdir, sep 
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer 

class MyHandler(BaseHTTPRequestHandler): 

# I WANT TO EXTRACT imsi parameter here and send a success response to 
# back to the client. 
def do_GET(self): 
    try: 
     if self.path.endswith(".html"): 
      #self.path has /index.htm 
      f = open(curdir + sep + self.path) 
      self.send_response(200) 
      self.send_header('Content-type','text/html') 
      self.end_headers() 
      self.wfile.write("<h1>Device Static Content</h1>") 
      self.wfile.write(f.read()) 
      f.close() 
      return 
     if self.path.endswith(".esp"): #our dynamic content 
      self.send_response(200) 
      self.send_header('Content-type','text/html') 
      self.end_headers() 
      self.wfile.write("<h1>Dynamic Dynamic Content</h1>") 
      self.wfile.write("Today is the " + str(time.localtime()[7])) 
      self.wfile.write(" day in the year " + str(time.localtime()[0])) 
      return 

     # The root 
     self.send_response(200) 
     self.send_header('Content-type','text/html') 
     self.end_headers() 

     lst = list(sys.argv[1]) 
     n = lst[len(lst) - 1] 
     now = datetime.datetime.now() 

     output = cStringIO.StringIO() 
     output.write("<html><head>") 
     output.write("<style type=\"text/css\">") 
     output.write("h1 {color:blue;}") 
     output.write("h2 {color:red;}") 
     output.write("</style>") 
     output.write("<h1>Device #" + n + " Root Content</h1>") 
     output.write("<h2>Device Addr: " + sys.argv[1] + ":" + sys.argv[2] + "</h1>") 
     output.write("<h2>Device Time: " + now.strftime("%Y-%m-%d %H:%M:%S") + "</h2>") 
     output.write("</body>") 
     output.write("</html>") 

     self.wfile.write(output.getvalue()) 

     return 

    except IOError: 
     self.send_error(404,'File Not Found: %s' % self.path) 
+0

क्या आपको 'GET' अनुरोध' के साथ भेजा गया 'args' नहीं मिलता है? – aayoubi

+0

संबंधित: https://stackoverflow.com/questions/2490162/parse-http-get-and-post-parameters-from-basehttphandler –

उत्तर

23

आप urlparse का उपयोग कर GET अनुरोध की क्वेरी को पार्स कर सकते हैं, तो क्वेरी स्ट्रिंग अलग हो गए।

from urlparse import urlparse 
query = urlparse(self.path).query 
query_components = dict(qc.split("=") for qc in query.split("&")) 
imsi = query_components["imsi"] 
# query_components = { "imsi" : "Hello" } 

# Or use the parse_qs method 
from urlparse import urlparse, parse_qs 
query_components = parse_qs(urlparse(self.path).query) 
imsi = query_components["imsi"] 
# query_components = { "imsi" : ["Hello"] } 

आप एक बहुत कम स्तर सर्वर का उपयोग कर

curl http://your.host/?imsi=Hello 
+1

और एक खोल विशेष चरित्र है ... बचने की आवश्यकता है ताकि आपका कर्ल कमांड दोनों पास हो पैरा –

+0

बेशक, स्पॉट करने के लिए धन्यवाद =) –

+1

पायथन 3 के साथ, 'urllib.parse आयात urlparse' स्रोत से' का उपयोग करें: https://stackoverflow.com/a/5239594/4669135 –

9

BaseHTTPServer है इसकी पुष्टि कर सकते हैं। आम तौर पर आप एक वास्तविक वेब ढांचे का उपयोग करना चाहते हैं जो आपके लिए इस प्रकार का कड़वाहट काम करता है, लेकिन जब से आपने पूछा ...

पहले यूआरएल पार्सिंग लाइब्रेरी आयात करें। पायथन 2 में, एक्स यह urlparse है। (Python3 में, आप urllib.parse का उपयोग करेंगे)

import urlparse 

फिर, अपने do_get विधि में, क्वेरी स्ट्रिंग पार्स।

imsi = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('imsi', None) 
print imsi # Prints None or the string value of imsi 

इसके अलावा, आप अपने ग्राहक कोड में urllib का उपयोग किया जा सकता है और यह शायद एक बहुत आसान होगा।

0

cgi मॉड्यूल में FieldStorage कक्षा शामिल है जिसका उपयोग सीजीआई संदर्भ में किया जाना चाहिए, लेकिन आपके संदर्भ में आसानी से उपयोग किया जा रहा है।