我正在尝试向使用套接字创建的服务器发送http请求,并尝试获取响应。当我尝试使用POST男人时,它很好。但是当我使用Frisby.js尝试它时,它显示以下错误。这是我服务器的代码。
__author__ = 'vimukthi'
import socket
import MySQLdb
import json
mstr = ""
c = ""
data1 = 0
s = ""
con = 0
def myResponse():
if con == 1:
print(mstr)
if mstr == 'GET ':
response = json.dumps({'200':'OK'})
message = response.encode('utf-8')
print(message)
c.send(message)
elif mstr == 'POST':
if data1 == 1:
response = json.dumps({'status':'2000','desc':'success'})
message = response.encode('utf-8')
print(message)
c.send(message)
else:
response = json.dumps({'status':'4001','desc':'id_not_found'})
message = response.encode('utf-8')
print(message)
c.send(message)
c.close()
else:
c.send("No data to display!")
return
def mySocket():
global s
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(5)
return
def myMethod():
mySocket()
while True:
global c
c, addr = s.accept()
print ('Got connection from', addr)
request = str(c.recv(addr[1]))
print(request)
global mstr
mstr1 = request.split('\n')[0]
mstr = mstr1[0:4]
if mstr == "POST":
id1 = request.split('\n')[-1]
method1 = request.split(',')[1]for frisby
id = id1.split(':')[1]
msg = method1.split(':')[1]
print(msg)
id = id[0] #[1] for postman, [0] for frisby
print(id)
elif mstr == "GET ":
method, message, version = mstr1.split()
msg = str(message)
print(mstr)
print(msg[2:])
id = msg[5]
print(id)
try:
db = MySQLdb.connect(host='127.0.0.1', user='root', passwd='0112608198', db='dumpDB')
cur = db.cursor()
cur.execute("select count(id) from user where id="+id+"")
value = cur.fetchall()
data = str(value[0])
global data1
data1 = int(data[1])
print(data1)
cur.close()
db.close()
global con
con = 1
except:
c.send("please try again later")
con = 0
myResponse()
return
myMethod()
这是我的Frisby.js代码。
var frisby = require('/home/vimukthi/Area51/installations/node_modules/frisby');
frisby.create('GET request to my server')
//.get('http://vimukthi-pc:12345?id=2&method=success')
.post('http://vimukthi-pc:12345',{"id" : 1,"method" : "check"},{json:true})
.inspectJSON()
.timeout(100000)
.toss()
任何人都可以帮我解决这个问题。
错误的屏幕截图。