간단한 HTTP 클라이언트와 서버를 파이썬으로 작성했습니다. 아래의 첫 번째 코드 스 니펫은 imsi 매개 변수와 함께 HTTP get 요청을 보내는 방법을 보여줍니다. 두 번째 코드 스 니펫에서는 서버 측에서 doGet 함수 구현을 보여줍니다. 내 질문은 서버 코드에서 imsi 매개 변수를 추출하여 클라이언트에게 응답을 보내어 imsi가 유효하다는 클라이언트에 신호를 보내는 방법입니다. 감사.서버 측에서 HTTP GET 입력 매개 변수를 파이썬에서 처리합니다.
P .: 클라이언트가 요청을 성공적으로 전송했음을 확인했습니다.
클라이언트 코드
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()
당신은 urlparse 사용하여 GET 요청의 쿼리를 구문 분석 할 수
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)
'GET '요청과 함께'args'를 보내지 않으셨습니까? – aayoubi
관련 : https://stackoverflow.com/questions/2490162/parse-http-get-and-post-parameters-from-basehttphandler –