我正在尝试向使用套接字创建的服务器发送 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()
谁能帮我解决这个问题。
错误的屏幕截图。